title: lisp simplify polynomials
body: <p>I'm writting a program that simplifies polynimials, only addition and multiplication for now.</p>
  
  <p>I've been slamming my head agains the keyboard for hours now and figure it was time to ask for some help.</p>
  
  <pre><code>(defun simplify (lis)
      (if (eq (car lis) '+)
          (cons '+ (simplify-addition (cdr lis)))
          (if (eq (car lis) '*)
              (cons '* (simplify-multiplication (cdr lis))) 
          )
      )
  )
  (defun simplify-addition (lis)
      (if (not (null lis))
          (if (listp (car lis))
              (list (simplify (car lis)) (simplify-addition (cdr lis)))
              (if (numberp (car lis))
                  (if (eq (car lis) 0)
                      (simplify-addition (cdr lis))
                      (if (null (cdr lis))
                          lis
                          (cons (car lis) (simplify-addition (cdr lis)))
                      )
                  )
                  (if (eq (car lis) '+)
                      (list (car lis) (simplify-addition (cdr lis)))
                      (if (eq (car lis) '*)
                          (list (car lis) (simplify-addition (cdr lis)))
                          lis
                      )
                  )
              )
          )
      )
  )
  
  (defun simplify-multiplication (lis)
      (if (not (null lis))
          (if (listp (car lis))
              (if (find 0 (car lis))
                  0
                  (list (simplify (car lis)) (simplify-multiplication (cdr lis)))
              )
              (if (numberp (car lis))
                  (if (null (cdr lis))
                      lis
                      (cons (car lis) (simplify-multiplication (cdr lis)))
                  )
                  (if (eq (car lis) '+)
                      (list (car lis) (simplify-multiplication (cdr lis)))
                      (if (eq (car lis) '*)
                          (list (car lis) (simplify-multiplication (cdr lis)))
                          lis
                      )
                  )
              )
          )
      )
  )
  </code></pre>
  
  <p>This is what should happen:</p>
  
  <pre><code>(simplify ‘(+  x  ( + 0 3 )  ( * 1 5 )  ( * ( * x  y  z ) 0 )  ))  --&gt;  ( + x 3 5 )
  (simplify ‘(* (+ 6  0)  (* 1 6 2)))  --------------------------------&gt;  (* 6 (* 6 2))
  </code></pre>
  
  <p>but instead i either get the same polynomial i sent in, or something completely off</p>
  
  <p>EDIT:
  The simplification that i need is to remove 0 from additions, so that:</p>
  
  <pre><code>(+ 3 0)   --&gt; 3
  (+ 4 0 6) --&gt; (+ 4 6)
  </code></pre>
  
  <p>and the multiplication with zero are removed</p>
  
  <pre><code>(* 6 0 7) --&gt; 0 
  </code></pre>