Created
December 11, 2012 21:23
-
-
Save mnicky/4262242 to your computer and use it in GitHub Desktop.
McCarthy's LISP with macros
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
| ;source: http://ideone.com/lEx0T | |
| ;explanation: http://stackoverflow.com/a/3685147 | |
| (defun null. (x) | |
| (eq x '())) | |
| (defun and. (x y) | |
| (cond (x (cond (y 't) ('t '()))) | |
| ('t '()))) | |
| (defun not. (x) | |
| (cond (x '()) | |
| ('t 't))) | |
| (defun append. (x y) | |
| (cond ((null. x) y) | |
| ('t (cons (car x) (append. (cdr x) y))))) | |
| (defun list. (x y) | |
| (cons x (cons y '()))) | |
| (defun pair. (x y) | |
| (cond ((and. (null. x) (null. y)) '()) | |
| ((and. (not. (atom x)) (not. (atom y))) | |
| (cons (list. (car x) (car y)) | |
| (pair. (cdr x) (cdr y)))))) | |
| (defun assoc. (x y) | |
| (cond ((null. y) '()) | |
| ((eq (caar y) x) (cadar y)) | |
| ('t (assoc. x (cdr y))))) | |
| (defun eval. (e a) | |
| (cond | |
| ((atom e) (assoc. e a)) | |
| ((atom (car e)) | |
| (cond | |
| ((eq (car e) 'quote) (cadr e)) | |
| ((eq (car e) 'atom) (atom (eval. (cadr e) a))) | |
| ((eq (car e) 'eq) (eq (eval. (cadr e) a) | |
| (eval. (caddr e) a))) | |
| ((eq (car e) 'car) (car (eval. (cadr e) a))) | |
| ((eq (car e) 'cdr) (cdr (eval. (cadr e) a))) | |
| ((eq (car e) 'cons) (cons (eval. (cadr e) a) | |
| (eval. (caddr e) a))) | |
| ((eq (car e) 'cond) (evcon. (cdr e) a)) | |
| ('t (eval. (cons (assoc. (car e) a) | |
| (cdr e)) | |
| a)))) | |
| ((eq (caar e) 'label) | |
| (eval. (cons (caddar e) (cdr e)) | |
| (cons (list. (cadar e) (car e)) a))) | |
| ((eq (caar e) 'lambda) | |
| (eval. (caddar e) | |
| (append. (pair. (cadar e) (evlis. (cdr e) a)) | |
| a))) | |
| ((eq (caar e) 'macro) | |
| (cond | |
| ((eq (cadar e) 'lambda) | |
| (eval. (eval. (car (cdddar e)) | |
| (cons (list. (car (caddar e)) (cadr e)) a)) | |
| a)))))) | |
| (defun evcon. (c a) | |
| (cond ((eval. (caar c) a) | |
| (eval. (cadar c) a)) | |
| ('t (evcon. (cdr c) a)))) | |
| (defun evlis. (m a) | |
| (cond ((null. m) '()) | |
| ('t (cons (eval. (car m) a) | |
| (evlis. (cdr m) a))))) | |
| ;(let ((e '((macro lambda (x) (list. (quote car) (list. (quote cdr) x))) | |
| ; (cons (quote x) (cons (quote y) nil)))) | |
| ; (bindings | |
| ; ;; Emacs 22 (but `symbol-function' is not portable): | |
| ; ; `((list. ,(symbol-function 'list.))))) | |
| ; ;; Expanded version of same: | |
| ; '((list. (lambda (x y) (cons x (cons y (quote nil)))))))) | |
| ; (eval. e bindings))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment