Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sealed abstract class Expression
case class X() extends Expression
case class Const(value : Int) extends Expression
case class Add(left : Expression, right : Expression) extends Expression
case class Mult(left : Expression, right : Expression) extends Expression
case class Neg(expr : Expression) extends Expression
def eval(e : Expression, x : Int) : Int = e match {
case X() => x
case Const(c) => c
case Add(l, r) => eval(l, x) + eval(r, x)
case Mult(l, r) => eval(l, x) * eval(r, x)
case Neg(expr) => - eval(expr, x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment