(I'm the second from the left)
We expanded the interpreter to allow more cases:
(define eval-expr
(lambda (expr env)
(pmatch expr
[`(if ,c ,p ,a)
(if (eval-expr c env)
(eval-expr p env)
(eval-expr a env))]
[`,x (guard (or (boolean? x) (number? x)))
x]
[`,x (guard (symbol? x))
(env x)]
[`(lambda (,x) ,body)
(lambda (arg)
(eval-expr body (lambda (y)
(if (eq? x y)
arg
(env y)))))]
[`(,rator ,rand1 ,rand2)
((eval-expr rator env)
(eval-expr rand1 env)
(eval-expr rand2 env))]
[`(,rator ,rand)
((eval-expr rator env)
(eval-expr rand env))])))
if
, boolean
s, number
s, and a function call with two parameters, for example (+ 41 1)
.
We also called it with a more complicated function:
(eval-expr '(((lambda (rf)
(lambda (n)
((rf rf) n)))
(lambda (!)
(lambda (n)
(if (zero? n)
1
(* n ((! !) (sub1 n)))))))
5)
(lambda (arg)
(cond
((eq? arg 'zero?) zero?)
((eq? arg 'sub1) sub1)
((eq? arg '*) *)
((eq? arg '+) +)
(else (environment arg)))))
I am going to talk about this in a future blog post.