using Haskell 1 Define a function called addpoly which takes
using Haskell.
1. Define a function called addpoly which takes two polynomials as input, and outputs the sum of the polynomials. The sum of two polynomials is the sum of the coefficients for terms with matching exponents. Make sure that the output has no terms with 0 as the coefficient.
You can break this into several cases:
a) Both polynomials have the same exponent
b) The first polynomial has a larger exponent
c) The second polynomial has a larger exponent
d) Both polynomials are 0
2. Define a function called multpoly which takes two polynomials as input, and outputs the product of the polynomials. The product of two polynomials is the sum of the result of multiplying a term from one polynomial with the other polynomial. The product of a term and polynomial is a polynomial with products of the coefficients and sums of the exponents. Remember again to only include output terms with nonzero coefficients. This can be implemented using the addpoly function to help.
Solution
{- Here I used tuple where first argument is the exponent of x
 and second argument is coefficient and it will be arranged in
 increasing order of exponent. So suppose our polynomial
 is 3*x^4 + 5*x^2 + 3, then it will be represented as
 [(0,3),(2,5),(4,3)]
 -}
type Poly = [(Int,Int)]
 addPoly :: Poly -> Poly -> Poly
 addPoly [] y = y
 addPoly x [] = x
 addPoly ((a1,b1):x) ((a2,b2):y)
 | a1 == a2 = ((a1,b1+b2):(addPoly x y)) -- when the exponent of two polynomials are same
 | a1 < a2 = ((a1,b1):(addPoly x ((a2,b2):y))) -- when first polynomial has smaller exponent
 | a1 > a2 = ((a2,b2):(addPoly ((a1,b1):x) y)) -- when second polynomial has smaller exponent
-- filter out the exponent who has coefficient value 0.
 main = do
 putStr $ show $ filter((/= 0).snd) $ addPoly [(0,3),(2,5), (4,3)] [(0,1),(1,-1)]
I have solved the first question with clear explaination, if you have any doubt you can ask in comment section.

