Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created October 21, 2014 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmeredith/506aad8a13fca5a7e190 to your computer and use it in GitHub Desktop.
Save kevinmeredith/506aad8a13fca5a7e190 to your computer and use it in GitHub Desktop.
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