Write a function that multiplies two polynomials in HASKELL Given: data Poly a = P [a] deriving (Show, Eq) multPoly :: (Num a, Eq a) => Poly a -> Poly a -> Poly a For example: > multPoly (P [-1,0,1]) (P [0,1]) P [0,-1,0,1] > multPoly (P [-1,0,1]) (P [2]) P [-2,0,2] > multPoly (P [1,1]) (P [-1,1]) P [-1,0,1] > multPoly (P [18,0,3]) (P []) P [] Before writing multPoly, you may find it helpful to write a function that increases the degree of a polynomial by multiplying it by x. E.g., mapping x^2−1 to ?^3−x. Seeing how this function, scale, and addPoly are written should give you a sense of how to construct multPoly. As with addPoly, multPoly is easier to write as a combination of simpler functions. Your implementation of multPoly should have this property: multPoly p q $$ x == (p $$ x) * (q $$ x) Write a function that adds two polynomials. Ensure that the polynomial produced matches the requirements. addPoly :: (Num a, Eq a) => Poly a -> Poly a -> Poly a
Write a function that multiplies two polynomials in HASKELL
Given:
data Poly a = P [a] deriving (Show, Eq)
multPoly :: (Num a, Eq a) => Poly a -> Poly a -> Poly a
For example:
> multPoly (P [-1,0,1]) (P [0,1])
P [0,-1,0,1]
> multPoly (P [-1,0,1]) (P [2])
P [-2,0,2]
> multPoly (P [1,1]) (P [-1,1])
P [-1,0,1]
> multPoly (P [18,0,3]) (P [])
P []
Before writing multPoly, you may find it helpful to write a function that increases the degree of a polynomial by multiplying it by x. E.g., mapping x^2−1 to ?^3−x. Seeing how this function, scale, and addPoly are written should give you a sense of how to construct multPoly. As with addPoly, multPoly is easier to write as a combination of simpler functions.
Your implementation of multPoly should have this property: multPoly p q $$ x == (p $$ x) * (q $$ x)
Write a function that adds two polynomials. Ensure that the polynomial produced matches the requirements.
addPoly :: (Num a, Eq a) => Poly a -> Poly a -> Poly a
For example:
> addPoly (P [1]) (P [-1,1])
P [0,1]
> addPoly (P [17]) (P [0,0,0,1])
P [17,0,0,1]
>addPoly (P [1,-1]) (P [0,1])
P [1]
Trending now
This is a popular solution!
Step by step
Solved in 2 steps