Streams are infinite lists. Most operations on streams are completely analogous to the definition in Data.List.
- data Stream a = Cons a (Stream a)
- head :: Stream a -> a
- tail :: Stream a -> Stream a
- map :: (a -> b) -> Stream a -> Stream b
- intersperse :: a -> Stream a -> Stream a
- iterate :: (a -> a) -> a -> Stream a
- repeat :: a -> Stream a
- cycle :: [a] -> Stream a
- unfold :: (c -> (a, c)) -> c -> Stream a
- take :: Int -> Stream a -> [a]
- drop :: (Num a, Ord a) => a -> Stream a1 -> Stream a1
- splitAt :: Int -> Stream a -> ([a], Stream a)
- takeWhile :: (a -> Bool) -> Stream a -> [a]
- dropWhile :: (a -> Bool) -> Stream a -> Stream a
- span :: (a -> Bool) -> Stream a -> ([a], Stream a)
- break :: (a -> Bool) -> Stream a -> ([a], Stream a)
- filter :: (a -> Bool) -> Stream a -> Stream a
- partition :: (a -> Bool) -> Stream a -> (Stream a, Stream a)
- isPrefixOf :: Eq a => [a] -> Stream a -> Bool
- (!!) :: Int -> Stream a -> a
- zip :: Stream a -> Stream b -> Stream (a, b)
- zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
- unzip :: Stream (a, b) -> (Stream a, Stream b)
- words :: Stream Char -> Stream String
- unwords :: Stream String -> Stream Char
- lines :: Stream Char -> Stream String
- unlines :: Stream String -> Stream Char
- listToStream :: [a] -> Stream a
- streamToList :: Stream a -> [a]
The type of streams
An infinite sequence.
Apply a function uniformly over all elements of a sequence.
x function produces the infinite sequence
of repeated applications of
iterate f x = [x, f x, f (f x), ..]
xs returns the infinite repetition of
cycle [1,2,3] = Cons 1 (Cons 2 (Cons 3 (Cons 1 (Cons 2 ...
The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite.
splitAt function takes an integer
n and a stream
| and returns a pair consisting of the prefix of
xs of length
n and the remaining stream immediately following this prefix.
xs returns the longest prefix of
xs that satisfies
p, together with the remainder of the stream.
xs, removes any elements from
xs that do not satisfy
partition function takes a predicate
p and a stream
xs, and returns a pair of streams. The first stream corresponds
to the elements of
xs for which
p holds; the second stream
corresponds to the elements of
xs for which
p does not hold.
isPrefix function returns
True if the first argument is a prefix of the second.
xs !! n returns the element of the stream
xs at index
n. Note that the head of the stream has index 0.
Zipping and unzipping streams
zip function takes two streams and returns a list of corresponding pairs.
Functions on streams of characters
words function breaks a stream of characters into a stream of words,
which were delimited by white space.
lines function breaks a stream of characters into a list
of strings at newline characters. The resulting strings do not
Converting to and from an infinite list
listToStream converts an infinite list to a
stream. Passing a finite list will result in an error.