Last active
March 10, 2016 10:41
-
-
Save semanticer/b448eb0d0e3ab9d6ba40 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
trait Exp { | |
def eval: Int | |
} | |
case class Oper(op: (Int, Int) => Int, left: Exp, right: Exp) extends Exp { | |
override def eval: Int = op(left.eval, right.eval) | |
} | |
case class Num(value: Int) extends Exp { | |
override def eval: Int = value | |
} | |
object Exp { | |
def eval(exp: Exp): Int = { | |
exp match { | |
case Oper(op, l, r) => | |
op(eval(r), eval(l)) | |
case Num(v) => v | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment