lisp

Table of Contents

1. Apply-Eval

Maxwell's equations of Software (Alan Kay in some Interview)

(define (apply fn x a)
    (cond
      ((atom fn)
       (cond
         ((eq fn CAR)  (caar x))
         ((eq fn CDR)  (cdar x))
         ((eq fn CONS) (cons (car x) (cadr x)))
         ((eq fn ATOM) (atom (car x)))
         ((eq fn EQ)   (eq (car x) (cadr x)))
         (#t           (apply (eval fn a) x a))))
      ((eq (car fn) LAMBDA)
       (eval (caddr fn) (pairlis (cadr fn) x a)))
      ((eq (car fn) LABEL)
       (apply (caddr fn) x
              (cons (cons (cadr fn) (caddr fn)) a)))))

(define (eval e a)
    (cond
      ((atom e) (cdr (assoc e a)))
      ((atom (car e))
       (cond ((eq (car e) QUOTE) (cadr e))
             ((eq (car e) COND)  (evcon (cdr e) a))
             (#t                 (apply (car e) (evlis (cdr e) a) a))))
      (#t       (apply (car e) (evlis (cdr e) a) a))))
Tags::lisp: