Homework

oleg@pobox.com oleg@pobox.com
Mon, 25 Aug 2003 18:50:42 -0700 (PDT)


> Here's one: figure out what the following does :-)

> puzzle = (!!) $ map (1:) $ iterate (s (lzw (+)) (1:)) [] where
>     s f g x = f x (g x)
>     lzw op xs [] = xs
>     lzw op [] ys = ys
>     lzw op (x:xs) (y:ys) = op x y : lzw op xs ys


Can be written simpler

puzzle = (!!) $ iterate (s (lzw (+)) (0:)) [1] where
    s f g x = f x (g x)
    lzw op [] ys = ys
    lzw op (x:xs) (y:ys) = op x y : lzw op xs ys

Incidentally, a small change gives a different series:

puzzle1 = (!!) $ iterate (s ((lzw (+)).(0:)) (1:)) [] where
    s f g x = f x (g x)
    lzw op [] ys = ys
    lzw op (x:xs) (y:ys) = op x y : lzw op xs ys


can you tell which? Without loading into GHCi? 

Finally, how can we possibly live without the following:

puzzle2 = (!!) $ iterate (s ((lzw (+)).(1:).(0:)) (0:)) [1,1] where
    s f g x = f x (g x)
    lzw op xs [] = []
    lzw op (x:xs) (y:ys) = op x y : lzw op xs ys