playground/haskell/unsorted/coinductive-type.hs

20 lines
415 B
Haskell

data Stream a =
Cons a (Stream a)
deriving Show
-- | Return first n elements from a stream as a list
streamTake
:: Int -- ^ Number of elements to take
-> Stream a -- ^ Input stream
-> [a] -- ^ List of elements from input stream
streamTake 0 xs = []
streamTake n (Cons x xs) = x : streamTake (n-1) xs
x = Cons 0 $ Cons 1 $ Cons 2 x
{-
- *Main> streamTake 5 x
- [0,1,2,0,1]
- }