#scala
def printTerm(term: Term) {
term match {
case Var(n) => print(n)
case Fun(x, b) => print("^" + x + ".") printTerm(b)
case App(f, v) => print("(") printTerm(f) print(" ") printTerm(v) print(")")
}
If you want to change selection, open document below and click on "Move attachment"
Case Classes - Scala Documentationfalse
It makes only sense to define case classes if pattern matching is used to decompose data structures. The following object defines a pretty printer function for our lambda calculus representation:
object TermTest extends scala.App {
<span>def printTerm(term: Term) {
term match {
case Var(n) =>
print(n)
case Fun(x, b) =>
print("^" + x + ".")
printTerm(b)
case App(f, v) =>
print("(")
printTerm(f)
print(" ")
printTerm(v)
print(")")
}
}
def isIdentityFun(term: Term): Boolean = term match {
case Fun(x, Var(y)) if x == y => true
case _ => false
}
val id = Fun("x", Var("x"))
val Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details