Revision as of 22:34, 2 December 2006

Useful Idioms that will blow your mind (unless you already know them :)

This collection is supposed to be comprised of short, useful, cool, magical examples, which should incite the reader's curiosity and (hopefully) lead him to a deeper understanding of advanced Haskell concepts. At a later time I might add explanations to the more obscure solutions. I've also started providing several alternatives to give more insight into the interrelations of solutions.

4.1 Polynomials

In abstract algebra you learn that polynomials can be used the same way integers are used given the right assumptions about their coefficients and roots. Specifically, polynomials support addition, subtraction, multiplication and sometimes division. It also turns out that one way to think of polynomials is that they are just lists of numbers (their coefficients). Here is one way to use lists to model polynomials. Since polynomials can support the same operations as integers, we model polynomials by making a list of numbers an instance of the Num type class.

-- First we tell Haskell that we want to make lists (or [a]) an instance of Num.-- We refer to this instance of the Num type class as Num [a].-- If you tried to use just:-- "instance Num [a] where"-- You'd get errors because the element type a is too general, too unconstrainted-- for what we need.-- So we add constraints to "a" by saying "Num a", this means whatever "a" is, it-- must be in the Num type class.instanceNum a =>Num[a]where-- Next, we have to implement all the operations that instances of Num support.-- A minimal set of operations is +, *, negate, abs, signum and fromInteger.
xs + ys =zipWith' (+) xs ys
where
zipWith' f [] ys = ys
zipWith' f xs [] = xs
zipWith' f (x:xs)(y:ys)= f x y : zipWith' f xs ys
-- We define a new version of zipWith that returns a list as long as the longest
-- of the two lists it is given. If we did not do this then when we add polynomials
-- the result would be truncated to the length of the shorter polynomial.
xs * ys = foldl1 (+) (padZeros partialProducts)
where
partialProducts = map (\x -> [x*y | y <- ys]) xs
padZeros = map (\(z,zs) -> replicate z 0 ++ zs) . (zip [0..])
-- This function is sort of hard to explain.... basically [1,2,3] should correspond
-- to the polynomial 1 + 2x + 3x^2. partialProducts does the steps of the multiplication
-- just like you would by hand when multiplying polynomials.
-- padZeros takes a list of polynomials and creates tuples of the form
-- (offset, poly). If you notice when you add the partial products by hand
-- that you have to shift the partial products to the left on each new line.
-- we accomplish this by padding by zeros at the beginning of the partial product.
-- Finally we use foldl1 to sum the partial products. Since they are polynomials
-- They are added by the definition of plus we already gave.
negate xs = map negate xs
abs xs = map abs xs -- is this reasonable?
signum xs = fromIntegral ((length xs)-1)
-- signum isn't really defined for polynomials, but polynomials do have a concept
-- of degree. We might as well reuse signum as the degree of the-- the polynomial. Notice that constants have degree zero.fromInteger x =[fromInteger x]-- This definition of fromInteger seems cyclical, it is left-- as an exercise to the reader to figure out why it is correct :)

The reader is encouraged to write a simple pretty printer that takes into account the many special cases of displaying a polynomial. For example, [1,3,-2, 0, 1,-1,0] should display as: -x^5 + x^4 - 2x^2 + 3x + 1

Other execrises for the reader include writing

polyApply ::(Num a)=>[a]-> a -> a

which evaluates the polynomial at a specific value or writing a differentiation function.