Hello! I want to add 2 polynomials, is working okay if the polynomials are the same dimension , but not if one has a different dimenions
(define (poly-addition poly1 poly2)
( define ( poly-addition-aux poly1 poly2 )
( cond (( and ( null? poly1 ) ( null? poly2 ))( list ))
(( null? poly1 ) poly2 )
(( null? poly2 ) poly1 )
( else
( cons (+ ( car poly1 ) ( car poly2 ))
( poly-addition-aux ( cdr poly1 ) ( cdr poly2 ))))))
( reverse ( poly-addition-aux ( reverse poly1 ) ( reverse poly2 ))))
(poly-add ’(3 7 8 ) ’(4 2 9 12)) ; expected ’(7 9 17 12) but I get ’(4 5 16 20)
Any hints please on where could be the bug ?
Hint (or at least something I would check): reverse
of the inputs
thank you so much!
Is correct to say that the head of each poly is degree 0 ? So, heads should pair up, trailing elements in the longer list are “summed” as if the other input were 0 ?
It depends on how you decided to represent the polynomials. Is (1 2 3)
representing 1+2x+3x^2
? If so you are almost right, the only exception to the rule is the polynomial 0, which has degree -infinity (usually).
@soegaard2 thank you for the reply! in my case, the first element is the constant
so ’(3 7 8 ) is
8 x^2 + 7 x + 3
’(4 2 9 12) is 12 x^3 + 9 x^2 + 2 x + 4