Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (22 sloc) 681 Bytes
module Stream where
import Prelude hiding (head, map, repeat, tail)
data Stream a = Cons a (Stream a)
instance Functor Stream where
fmap = map
instance Applicative Stream where
pure = repeat
(Cons f fs) <*> (Cons y ys) = Cons (f y) (fs <*> ys)
instance Monad Stream where
return = repeat
xs >>= f = join $ fmap f xs
where
join :: Stream (Stream a) -> Stream a
join (Cons xs xss) = Cons (head xs) (join (map tail xss))
head :: Stream a -> a
head (Cons x _) = x
map :: (a -> b) -> Stream a -> Stream b
map f (Cons x xs) = Cons (f x) (map f xs)
repeat :: a -> Stream a
repeat x = Cons x (repeat x)
tail :: Stream a -> Stream a
tail (Cons _ xs) = xs