---------------------------------------------------------------------- module TermPoly where ---------------------------------------------------------------------- -- COMP2600 -- Assignment 1, 2003 -- Skeleton script -- Represent a polynomial as a list of terms, where a term is -- represented as a coefficient, exponent pair. Terms with zero -- coefficients do not appear. The terms appear in decreasing -- order of exponent. type Coeff = Float type Expon = Int type Term = (Coeff, Expon) type Poly = [Term] ---------------------------------------------------------------------- add :: Poly -> Poly -> Poly add [] [] = [] add x [] = x add [] y = y add x y = (fst (unzip x)) + (fst (unzip y)) ---------------------------------------------------------------------- sub :: Poly -> Poly -> Poly sub x y = [] ---------------------------------------------------------------------- eval :: Float -> Poly -> Float eval x [] = 0 eval x p = ((head (fst (unzip p))) * (x ^ head (snd (unzip p)))) + (eval x (tail p)) ----------------------------------------------------------------------