Created
December 6, 2015 14:02
-
-
Save sternenseemann/2e16103b45c15cf8a75b to your computer and use it in GitHub Desktop.
Infinite Lists
This file contains hidden or 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
module Data.Infinite where | |
import Prelude hiding (iterate, take, drop) | |
data Infinite a = Cons a (Infinite a) deriving Show | |
simple :: a -> Infinite a | |
simple x = Cons x $ simple x | |
iterate :: a -> (a -> a) -> Infinite a | |
iterate x f = Cons (f x) (iterate (f x) f) | |
take :: Integer -> Infinite a -> [a] | |
take 0 _ = [] | |
take n (Cons x xs) = x : take (n - 1) xs | |
drop :: Integer -> Infinite a -> Infinite a | |
drop 0 xs = xs | |
drop n (Cons _ xs) = drop (n - 1) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment