Last active
August 29, 2015 13:57
-
-
Save ehaliewicz/9847170 to your computer and use it in GitHub Desktop.
Lambda calculus interpreter in common lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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