;Term format: (deg coef) (define p+ (lambda (poly1 poly2) (cond ((zero-poly? poly1) poly2) ((zero-poly? poly2) poly1) (else (let ( (n1 (degree poly1)) (n2 (degree poly2)) ) (cond ((> n1 n2) (let ( (a1 (leading-coef poly1)) (rest1 (rest-of-poly poly1)) ) (poly-cons n1 a1 (p+ rest1 poly2)) ) ) ((< n1 n2) (let ( (a2 (leading-coef poly2)) (rest2 (rest-of-poly poly2)) ) (poly-cons n2 a2 (p+ rest2 poly1)) ) ) (else (let ( (a1 (leading-coef poly1)) (a2 (leading-coef poly2)) (rest1 (rest-of-poly poly1)) (rest2 (rest-of-poly poly2)) ) (poly-cons n1 (+ a1 a2) (p+ rest1 rest2)) ) ) ) ) ) ) ) ) ;============== Unit Tests ====================== (load "test.scm") (load "poly-basic.scm") (define msg "poly-add") (test msg (p+ '((4 5)) '((1 2))) '((4 5) (1 2))) (test msg (p+ '((1 2)) '((4 5))) '((4 5) (1 2))) (test msg (p+ '((4 5) (3 -7) (1 2) (0 -4)) '((3 1) (2 6) (1 -3))) '((4 5) (3 -6) (2 6) (1 -1) (0 -4)))