Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example
- trait Expr
- case class Number(n: Int) extends Expr
- case class Sum(e1: Expr, e2: Expr) extends Expr
- case class Var(x:Int) extends Expr
- case class Prod(e1: Expr, e2: Expr) extends Expr
- object week4PatternMatching {
- def eval(e: Expr): Int = e match {
- case Number(n) => n
- case Sum(e1, e2) => eval(e1) + eval(e2)
- }
- def show(e:Expr): String = e match{
- case Number(n)=> "n"
- case Sum(l,r) => show(l) + "+" + show(r)
- case Prod(l,r)=> show(l) + "*" + show(l)
- }
- show(Sum(Number(1),Number(44)))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement