Question
Given the following outline, implement the following functions in Haskell. 1. The file contains the algebraic data type definition data Poly a = X |
Given the following outline, implement the following functions in Haskell.
1. The file contains the algebraic data type definition data Poly a = X | Coef a | Sum ( Poly a) ( Poly a) | Prod ( Poly a) ( Poly a) deriving Show
2. The file includes a function named polyValue of type Num a => Poly a -> a -> a such that polyValue p n is the value of p at n.
3. The file includes a function named polyDegree of type (Num a, Eq a) => Poly a -> Integer such that polyDegree p is the degree of p.
data Poly a = X | Coef a | Sum (Poly a) (Poly a) | Prod (Poly a) (Poly a) deriving Show
{- ----------------------------------------------------------------- - polyValue - ----------------------------------------------------------------- - Description: TODO add comments on polyValue here -} polyValue :: Num a => Poly a -> a -> a polyValue p n = error "TODO: implement polyValue"
{- ----------------------------------------------------------------- - polyDegree - ----------------------------------------------------------------- - Description: TODO add comments on polyDegree here -} polyDegree :: (Num a, Eq a) => Poly a -> Integer polyDegree p = error "TODO: implement polyDegree"
{- ----------------------------------------------------------------- - polyDeriv - ----------------------------------------------------------------- - Description: TODO add comments on polyDeriv here -} polyDeriv :: Num a => Poly a -> Poly a polyDeriv p = error "TODO: implement polyDeriv"
Here is some background information.
Let C be a set of coefficients closed under addition and multiplication such as the integers Z, the rational numbers Q, or the real numbers R. A polynomial over C is a mathematical expression that is constructed from an indetermi- nant x and members of C by applying addition (+) and multiplication (*) operators. Let P be the set of polynomials over some C. The value of a polynomial p 2 P at c 2 C is the result of replacing the indeterminant x with c. For example, the value of (2 * x) + 4 at 3 is (2 * 3) + 4 = 10. Every polynomial p represents a polynomial function fp : C ! C that maps c 2 C to the value of p at c. For every p 2 P, there is a q 2 P that 1 has the form a_0 + a_1 * x^1 + a2 * x2 + ... + a_m * x^m; where a0; a1; : : : ; am 2 C, am 6= 0, xi is an abbreviation for x x (i times), and parentheses have been suppressed, such that fp and fq are the same function. q is called the standard form of p. The degree of p is m, the largest exponent appearing in q. For example, the standard form of (x + 1) * (x + 2) is 2 + 3 * x^1 + x^2 and the degree of (x + 1) * (x + 2) is 2.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started