Stream-0.1: A library for manipulating infinite lists.Source codeContentsIndex
Data.Stream
Contents
The type of streams
Basic functions
Stream transformations
Building streams
Extracting sublists
Sublist predicates
Indexing streams
Zipping and unzipping streams
Functions on streams of characters
Converting to and from an infinite list
Description
Streams are infinite lists. Most operations on streams are completely analogous to the definition in Data.List.
Synopsis
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]
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
data Stream a Source
An infinite sequence.
Constructors
Cons a (Stream a)
show/hide Instances
Basic functions
head :: Stream a -> aSource
Extract the first element of the sequence.
tail :: Stream a -> Stream aSource
Extract the sequence following the head of the stream.
Stream transformations
map :: (a -> b) -> Stream a -> Stream bSource
Apply a function uniformly over all elements of a sequence.
intersperse :: a -> Stream a -> Stream aSource
Building streams
iterate :: (a -> a) -> a -> Stream aSource

iterate f x function produces the infinite sequence of repeated applications of f to x.

 iterate f x = [x, f x, f (f x), ..]
repeat :: a -> Stream aSource
repeat x returns a constant stream, where all elements are equal to x.
cycle :: [a] -> Stream aSource

cycle xs returns the infinite repetition of xs:

 cycle [1,2,3] = Cons 1 (Cons 2 (Cons 3 (Cons 1 (Cons 2 ...
unfold :: (c -> (a, c)) -> c -> Stream aSource
The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite.
Extracting sublists
take :: Int -> Stream a -> [a]Source
take n xs returns the first n elements of xs.
splitAt :: Int -> Stream a -> ([a], Stream a)Source
The splitAt function takes an integer n and a stream xs | and returns a pair consisting of the prefix of xs of length | n and the remaining stream immediately following this prefix.
takeWhile :: (a -> Bool) -> Stream a -> [a]Source

drop n xs drops the first n elements off the front of the sequence xs.

takeWhile p xs returns the longest prefix of the stream xs for which the predicate p holds.

dropWhile :: (a -> Bool) -> Stream a -> Stream aSource
dropWhile p xs returns the suffix remaining after takeWhile p xs.
span :: (a -> Bool) -> Stream a -> ([a], Stream a)Source
span p xs returns the longest prefix of xs that satisfies p, together with the remainder of the stream.
break :: (a -> Bool) -> Stream a -> ([a], Stream a)Source
The break p function is equivalent to span not . p.
filter :: (a -> Bool) -> Stream a -> Stream aSource
filter p xs, removes any elements from xs that do not satisfy p.
partition :: (a -> Bool) -> Stream a -> (Stream a, Stream a)Source
The 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.
Sublist predicates
isPrefixOf :: Eq a => [a] -> Stream a -> BoolSource
The isPrefix function returns True if the first argument is a prefix of the second.
Indexing streams
(!!) :: Int -> Stream a -> aSource
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 :: Stream a -> Stream b -> Stream (a, b)Source
The zip function takes two streams and returns a list of corresponding pairs.
zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream cSource
The zipWith function generalizes zip. Rather than tupling the functions, the elements are combined using the function passed as the first argument to zipWith.
unzip :: Stream (a, b) -> (Stream a, Stream b)Source
The unzip function is the inverse of the zip function.
Functions on streams of characters
words :: Stream Char -> Stream StringSource
The words function breaks a stream of characters into a stream of words, which were delimited by white space.
unwords :: Stream String -> Stream CharSource
The unwords function is an inverse operation to words. It joins words with separating spaces.
lines :: Stream Char -> Stream StringSource
The lines function breaks a stream of characters into a list of strings at newline characters. The resulting strings do not contain newlines.
unlines :: Stream String -> Stream CharSource
The unlines function is an inverse operation to lines. It joins lines, after appending a terminating newline to each.
Converting to and from an infinite list
listToStream :: [a] -> Stream aSource
The listToStream converts an infinite list to a stream. Passing a finite list will result in an error.
streamToList :: Stream a -> [a]Source
The streamToList converts a stream into an infinite list.
Produced by Haddock version 2.1.0