Polynomial product function

Viewed 62

I'm an F# beginner trying to create a function mul to multiply two polynomials. I'm using three helper functions: add, mulX and mulC which respectively add polynomials, multiply by x and multiply by constant. The idea is that I'm representing my polynomials in lists, so [1;2;3] would be 1+2x+3x^2.

I've gotten the following definition as a guideline for combining the helper functions.

However, I can't seem to get it to work, even though (I think) I've followed the guideline completely.

I've added a screenshot of the code, helper functions and output here. I get 2+7x+13x^2+11x^3+3x^4, and I'm supposed to get 2+7x+12x^2+10x^3+2x^4+3*x^5.

I'm sorry if the format is bad, this is my first post.

Where did I go wrong? I've tested my helper functions, and they seem to work. Any help is appreciated!

1 Answers

After looking at your code, I think it's implemented correctly. You've just misrepresented the test. It should be:

mul [2;3;0;1] [1;2;3]

Note that the last term of the first polynomial is x3, and there's no x2 term:

2 + 3x + x3 = 2 + 3x + 0x2 + 1x3

An easy mistake to make.

Related