{{ message }}

Instantly share code, notes, and snippets.

# lazywithclass/blog-post.md

Last active Apr 29, 2017
[RC Diary] Scheme interpreter, lambda calculus, and wtf is this (-32)

## [RC Diary] Scheme interpreter, lambda calculus, and wtf is this (-32)

tl;dr:

(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)))))```