Professional Documents
Culture Documents
Solution HW04 of CIS 194 Haskell Course 2015
Solution HW04 of CIS 194 Haskell Course 2015
Solution HW04 of CIS 194 Haskell Course 2015
signum = undefined
-- Exercise 7 ----------------------------------------apply :: Num a => [a] -> a -> a
apply p n = fst $ foldl f (0, 0 :: Int) p where
f (result, degree) coef = (result + coef * n ^ degree, degree + 1)
applyP :: Num a => Poly a -> a -> a
applyP (P p) n = apply p n
-- Exercise 8 ----------------------------------------class Num a => Differentiable a where
deriv :: a -> a
nderiv :: Int -> a -> a
nderiv 0 = id
nderiv 1 = deriv
nderiv n
| n > 0 = deriv . (nderiv $ n-1)
| otherwise = undefined
-- Exercise 9 ----------------------------------------instance Num a => Differentiable (Poly a) where
deriv (P p) = P $ derivL p where
derivL [] = [0]
derivL [_] = [0]
derivL (_:cs) = fst $ foldl f ([], 1) cs where
f (dp, degree) coef = (dp ++ [coef * degree], degree+1)