I found this code that builds a list corresponding to the Thue-Morse sequence:
thueMorse :: [Int]
thueMorse = 0 : interleave (map (1-) thueMorse) (tail thueMorse)
where interleave (x:xs) ys = x : interleave ys xs
It's perfect and works wonders, but I cannot wrap my head around it. An example:
> take 8 thueMorse
[0,1,1,0,1,0,0,1]
If I define the interleave function globally and use it I get, and rightly so, an exception:
> let interleave (x:xs) ys = x : interleave ys xs
> interleave [1,2,3] [4,5,6]
[1,4,2,5,3,6*** Exception: <interactive>:29:5-47: Non-exhaustive patterns in function interleave
So, how does the above work? Is it because it's an infinite list so it's safe to interleave forever?