http://www.seas.upenn.edu/~cis194/spring13/hw/06-laziness.pdf
The question is about representing the ruler function
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, . . . where the nth element in the stream (assuming the first element corresponds to n = 1) is the largest power of 2 which evenly divides n
I have working solution using show to display the first 20 elements with interleaveStreams' function:
data Stream a = Cons a (Stream a)
streamToList :: Stream a -> [a]
streamToList (Cons a b) = a : streamToList b
instance Show a => Show (Stream a) where
show = show . take 20 . streamToList
streamRepeat :: a -> Stream a
streamRepeat a = Cons a (streamRepeat a)
ruler :: Stream Integer
ruler =
let s n = interleaveStreams' (streamRepeat n) (s (n+1))
in s 0
interleaveStreams :: Stream a -> Stream a -> Stream a
interleaveStreams (Cons x xs) (Cons y ys) = Cons x (Cons y (interleaveStreams xs ys))
interleaveStreams' :: Stream a -> Stream a -> Stream a
interleaveStreams' (Cons x xs) y = Cons x $ interleaveStreams' y xs
However I don't understand why ruler using the interleave function instead does not terminate with Show.