Skip to content

Instantly share code, notes, and snippets.

@sternenseemann
Created December 6, 2015 14:02
Show Gist options
  • Save sternenseemann/2e16103b45c15cf8a75b to your computer and use it in GitHub Desktop.
Save sternenseemann/2e16103b45c15cf8a75b to your computer and use it in GitHub Desktop.
Infinite Lists
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