Skip to content

Instantly share code, notes, and snippets.

@IwoHerka
Last active May 24, 2019 17:17
Show Gist options
  • Save IwoHerka/3742095bfa2f33a9dc9ed5ae0f63cd2f to your computer and use it in GitHub Desktop.
Save IwoHerka/3742095bfa2f33a9dc9ed5ae0f63cd2f to your computer and use it in GitHub Desktop.
Lisp's eval function
(label eval (lambda (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 (card 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 (pair (cadar e) (evlis (cdr 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) f)
(t (cons (eval (car m) a)
(evlis (car m) a)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment