4. Write a function that adds two polynomials. Ensure that the polynomial produced matches the requirements. addPoly :: (Num a, Eg 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] You may find it helpful to write addPoly using two helper functions: one to combine the lists of coefficients, and one to trim trailing zeroes. Your implementation of addPoly should have this property: addPoly p q $$ x == (p sS x) + (a Ss x)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question

Has to be in HASKELL

4. Write a function that adds two polynomials. Ensure that the polynomial produced matches the requirements.
addPoly :: (Num a, Eg 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]
You may find it helpful to write addPoly using two helper functions: one to combine the lists of coefficients, and one to trim trailing zeroes.
Your implementation of addPoly should have this property: addPoly p q $$ x == (p Ss x) + (a SS x)
Transcribed Image Text:4. Write a function that adds two polynomials. Ensure that the polynomial produced matches the requirements. addPoly :: (Num a, Eg 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] You may find it helpful to write addPoly using two helper functions: one to combine the lists of coefficients, and one to trim trailing zeroes. Your implementation of addPoly should have this property: addPoly p q $$ x == (p Ss x) + (a SS x)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning