Code: Select all
(defun findfactor (term)
(car term))
(defun findpower (term)
(cadr term))
(defun maketerm(factor power)
(list factor power))
(defun add-poly (poly1 poly2)
(if (null poly1) poly2
(if (null poly2) poly1
(let ( (term1 (car poly1)) (term2 (car poly2)) )
(if (= (findpower term1) (findpower term2))
(maketerm (+ (findfactor term1) (findfactor term2)) (findpower term1))
(if (> (findpower term1) (findpower term2))
(append poly1 poly2) (append poly2 poly1)))
(add-poly (cadr poly1) (cadr poly2))))))
(add-poly `( (2 2) ) `( (2 1) )) returns nil
With one two-term polynomial and one one-term polynomial -
(add-poly `( (2 2) (2 1)) `( (2 2) )) returns the second term of (2 1)
With two two-term polynomials -
(add-poly `( (5 2) (3 1)) `( (8 2) (9 1) )) gets an error that says "Cannot take CDR of 3"
All of these scenarios really confuse me. If I take the recursive call at the end away and just try to add two one-term polynomials, it works fine. So I know the if statement is right...I just can't seem to get how to correctly call this again to work for polynomials that have more than one term. Any help is appreciated, thanks.