Created
October 21, 2014 02:33
-
-
Save kevinmeredith/506aad8a13fca5a7e190 to your computer and use it in GitHub Desktop.
StackOverflow-ing `ruler` Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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