daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Expr(f: Function1[Map[Symbol,Int], Int]) {
  2.   def apply(env: Map[Symbol, Int]): Int = f(env)
  3. }
  4.  
  5. object Lambda extends App {
  6.   val number: Int => Expr = num => new Expr(env => num)
  7.   val variable = (id: Symbol) => new Expr(env => env(id))
  8.   val add = (a: Expr, b: Expr) => new Expr(env => a(env) + b(env))
  9.   val multiply = (a: Expr, b: Expr ) => new Expr(env => a(env) * b(env))
  10.  
  11.   val environment = Map('a -> 1, 'b -> 2, 'c -> 3)
  12.   val expr_tree = add(variable('a), multiply(number(2), variable('b)))
  13.  
  14.   println(expr_tree(environment))
  15. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top