Simple Partial Evaluator

(defun mix (code env)
 (let (args test fn)
   (if (constantp code)           ; a constant
     code                         ;   evaluates to itself
     (if (symbolp code)           ; a variable
       (if (assoc code env)       ;   bound to a constant
           (cdr (assoc code env)) ; evals to that constant
           code)                  ; else to itself
       (if (consp code)
            (setq fn (car code))
            (if (eq fn 'if)         ; if is handled
                (progn              ;     specially
                  (setq test (mix (cadr code) env))
                  (if (eq test t)            ; if true
                      (mix (caddr code) env) ; then part
                      (if (eq test nil)      ; if false
                          (mix (cadddr code) env) ; else
                          (cons 'if
                            (cons test
                              (mapcar #'(lambda (x)
                                          (mix x env))
                                      (cddr code)))))))

Contents    Page-10    Prev    Next    Page+10    Index