Date: Mon, 02 Dec 1996 14:42:34 GMT Server: NCSA/1.4.2 Content-type: text/html
CSE 415 Prog. Assignment 2 Solution
Note that by simplifying the sub expressions into a1 and a2, we avoid the
problem of having to run the simplify algorithm multiple times.

(defun simplify (e)
  (cond ((atom e) e)
        (t (let ((a1 (simplify (arg1 e)))
                 (a2 (simplify (arg2 e))))
             (cond ((equal (op e) '*) (cond ((equal a1 0) 0)
                                               ((equal a2 0) 0)
                                               ((equal a1 1) a2)
                                               ((equal a2 1) a1)
                                               (t `(* ,a1 ,a2))))
                   ((equal (op e) '+) (cond ((equal a1 0) a2)
                                               ((equal a2 0) a1)
                                               (t `(+ ,a1 ,a2))))
                   ((equal (op e) '-) (cond ((equal a2 0) a1)
                                               (t `(- ,a1 ,a2))))
                   ((equal (op e) 'expt) (cond ((equal a2 1) a1)
                                                  (t `(expt ,a1 ,a2)))))))))

where op arg1 and arg2 extract the first, second, and third elements
of a list.