mihaidobri
2020-10-19 05:26:59

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


mihaidobri
2020-10-19 05:27:09

(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 ))))


mihaidobri
2020-10-19 05:28:08

(poly-add ’(3 7 8 ) ’(4 2 9 12)) ; expected ’(7 9 17 12) but I get ’(4 5 16 20)


mihaidobri
2020-10-19 05:28:19

Any hints please on where could be the bug ?


samdphillips
2020-10-19 05:30:50

Hint (or at least something I would check): reverse of the inputs


mihaidobri
2020-10-19 05:31:29

thank you so much!


mihaidobri
2020-10-19 06:09:50

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 ?


soegaard2
2020-10-19 06:18:00

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).


mihaidobri
2020-10-19 06:35:22

@soegaard2 thank you for the reply! in my case, the first element is the constant


mihaidobri
2020-10-19 06:35:29

so ’(3 7 8 ) is


mihaidobri
2020-10-19 06:35:39

8 x^2 + 7 x + 3


mihaidobri
2020-10-19 06:36:32

’(4 2 9 12) is 12 x^3 + 9 x^2 + 2 x + 4