Skip to content

Instantly share code, notes, and snippets.

@tca
Created November 24, 2014 01:25
Show Gist options
  • Save tca/6aae08b905487f74cfbb to your computer and use it in GitHub Desktop.
Save tca/6aae08b905487f74cfbb to your computer and use it in GitHub Desktop.
(define (my-eval exp env)
(match exp
((int i) => i)
((add a b) => (+ (my-eval a env) (my-eval b env)))
((abs p b) => (list 'cls exp env))
((var a) => (cond ((assoc a env) => cdr)
(else (error "unbound variable" a))))
((app f a) => (match (my-eval f env)
((cls (abs p b) cenv) => (my-eval b (cons (cons p (my-eval a env)) cenv)))
(else (error "not a valid function: " f))))
(else (error "invalid expression: " exp))))
(my-eval '(app (app (abs a (abs b (add (var a) (var b)))) (int 1)) (int 2)) '())
;; => 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment