(defun coef (t)
(if (equal t ())
()
(car t)
)
)
(defun var (t)
(if (atom t)
()
(car (cdr t))
)
)
(defun pow (t)
(if (atom t)
(progn (print "t is an atom")
())
(if (atom (cdr t))
(progn (print "cdr t is an atom")
()
(car (cdr (cdr t))))
)
)
)
(defun pd (p wrt) ;derivative function taking a polynomial, and a variable to differentiate With Respect To
(progn (print p)
(cond ((atom p)
(progn (print "p atom")
()))
((atom (car p))
())
((equal (pow (car p)) 0)
(cons (list 0 0 0) ;important to note that in our system
(pd (cdr p) wrt) ;constant terms may be of the form x^0
)
)
((equal (var (car p)) wrt) ;if it's a term we want to differentiate
(progn (print "differentiate")
(cons (cons (* (coef (car p)) (pow (car p)))
(cons wrt
(if (nil (pow (car p)))
()
(- (pow (car p)) 1) ;differentiate it!
)
)
)
(pd (cdr p) wrt)
)
)
)
(t (cons (list 0 0 0)
(pd (cdr p) wrt)
) ;otherwise, it goes to zero
)
) ;note that after each term, we differentiate the next
)
) ;until we hit nil, at which point we stop