Created
September 18, 2008 06:58
-
-
Save hsak/11391 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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