Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
StackOverflow-ing `ruler` Function
data Stream a = Cons a (Stream a)
-- Show 30 items of the list rather than infinitity
instance Show a => Show (Stream a) where
show stream = show $ go stream 1
where go (Cons x rest) count
| count == 20 = []
| otherwise = x : go rest (count+1)
streamRepeat :: a -> Stream a
streamRepeat x = Cons x (streamRepeat x)
interleave :: Stream a -> Stream a -> Stream a
interleave (Cons x strX) (Cons y strY) = Cons x (Cons y (interleave strX strY))
-- overflows the stack without any output
ruler :: Stream Integer
ruler = ruler' 0
ruler' :: Integer -> Stream Integer
ruler' n = interleave (streamRepeat n) (ruler' (n+1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment