Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehaliewicz/9847170 to your computer and use it in GitHub Desktop.
Save ehaliewicz/9847170 to your computer and use it in GitHub Desktop.
Lambda calculus interpreter in common lisp
(defun interpret-lambda (expr &optional env)
(etypecase expr
(symbol (let ((res (assoc expr env))) (if res (cdr res) (error "Unbound symbol"))))
(list (case (car expr)
(lambda (list (car (second expr)) (third expr) env))
(otherwise
(let ((rand (interpret-lambda (second expr) env))
(rator (interpret-lambda (first expr) env)))
(interpret-lambda
(cadr rator)
(cons (cons (car rator) rand) (third rator)))))))))
CL-USER> (interpret-lambda '(((lambda (x) (lambda (y) x))
(lambda (xbound) xbound))
(lambda (ybound) ybound)))
(XBOUND XBOUND NIL) ;; ad-hoc lambda form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment