My code caused some sort of infinite recursion so I decided to look through this post (OP was doing the same problem as me: implementing the ruler function on an infinite Stream):
Implementing the ruler function using `streamInterleave`
My code looked like this:
data Stream a = Cons a (Stream a)
streamRepeat :: a -> Stream a
streamRepeat x = Cons x (streamRepeat x)
interleaveStreams :: Stream a -> Stream a -> Stream a
interleaveStreams (Cons x xs) (Cons y ys) = Cons x (Cons y (streamInterleave xs ys))
ruler :: Stream Integer
ruler = interleaveStreams (streamRepeat 0) (streamMap (+1) $ ruler)
Now, it turns out that if I change the definition of interleaveStreams to
interleaveStreams :: Stream a -> Stream a -> Stream a
interleaveStreams (Cons x xs) ys = Cons x (interleaveStreams ys xs)
and leave everything else as is, there is no infinite recursion and it works properly. As far as I know, the two versions of interleaveStreams are equivalent. Why does one of them hang, while the other one works properly?