Skip to content

Instantly share code, notes, and snippets.

@hsak
Created September 18, 2008 06:58
Show Gist options
  • Save hsak/11391 to your computer and use it in GitHub Desktop.
Save hsak/11391 to your computer and use it in GitHub Desktop.
abstract class Atom
case class Var(value: int) extends Atom
case class Add(left:Atom, right:Atom) extends Atom
case class Mul(left:Atom, right:Atom) extends Atom
object Case {
def main(args: Array[String]) = {
val exp = Add(Var(1),Var(2))
println(exp)
println(eval(exp))
}
def eval(exp:Atom):int = {
exp match {
case Var(a) => a
case Add(a,b) => eval(a) + eval(b)
case Mul(a,b) => eval(a) * eval(b)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment