Advertisement
Guest User

Untitled

a guest
Sep 4th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.67 KB | None | 0 0
  1. abstract class Tree
  2. case class Sum(l: Tree, r: Tree) extends Tree
  3. case class Var(n: String) extends Tree
  4. case class Const(v: Int) extends Tree
  5.  
  6. object TreeTest {
  7.  
  8.     type Environment = String => Int
  9.  
  10.     def eval(t: Tree, env: Environment): Int = t match {
  11.         case Sum(l,r) => eval(l, env) + eval(r, env)
  12.         case Var(n)   => env(n)
  13.         case Const(v) => v
  14.     }
  15.  
  16.     def main(args: Array[String]) {
  17.         val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Const(7), Var("y")))
  18.         val env: Environment = { case "x" => 5  case "y" => 7 }
  19.  
  20.         println("Expression: " + exp)
  21.         println("Evalutaion with x=5, y=7 " + eval(exp, env))
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement