|
|
|
|
|
Description |
Streams are infinite lists. Most operations on streams are
completely analogous to the definition in Data.List.
|
|
Synopsis |
|
|
|
|
The type of streams
|
|
|
An infinite sequence.
| Constructors | | Instances | |
|
|
Basic functions
|
|
|
The <:> operator is an infix version of the Cons
constructor.
|
|
|
Extract the first element of the sequence.
|
|
|
Extract the sequence following the head of the stream.
|
|
|
The inits function takes a stream xs and returns all the
finite prefixes of xs.
|
|
|
The tails function takes a stream xs and returns all the
suffixes of xs.
|
|
Stream transformations
|
|
|
Apply a function uniformly over all elements of a sequence.
|
|
|
intersperse y xs creates an alternating stream of
elements from xs and y.
|
|
|
Interleave two Streams xs and ys, alternating elements
from each list.
@[x1,x2,...] `interleave` [y1,y2,...] == [x1,y1,x2,y2,...]@
|
|
|
scanl yields a stream of successive reduced values from the
| left:
scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
|
|
|
scanl1 is a variant of scanl that has no starting value argument:
scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
|
|
Building streams
|
|
|
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 x returns a constant stream, where all elements are
equal to x.
|
|
|
cycle xs returns the infinite repetition of xs:
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.
|
|
Extracting sublists
|
|
|
take n xs returns the first n elements of xs.
Beware: passing a negative integer as the first argument will
cause an error.
|
|
|
drop n xs drops the first n elements off the front of
the sequence xs.
Beware: passing a negative integer as the first argument will
cause an error.
|
|
|
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.
Beware: passing a negative integer as the first argument will
cause an error.
|
|
|
takeWhile p xs returns the longest prefix of the stream
xs for which the predicate p holds.
|
|
|
dropWhile p xs returns the suffix remaining after
takeWhile p xs.
Beware: this function may diverge if every element of xs
satisfies p, e.g. dropWhile even (repeat 0) will loop.
|
|
|
span p xs returns the longest prefix of xs that satisfies
p, together with the remainder of the stream.
|
|
|
The break p function is equivalent to span not . p.
|
|
|
filter p xs, removes any elements from xs that do not satisfy p.
Beware: this function may diverge if there is no element of
xs that satisfies p, e.g. filter odd (repeat 0) will loop.
|
|
|
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.
Beware: One of the elements of the tuple may be undefined. For
example, fst (partition even (repeat 0)) == repeat 0; on the
other hand snd (partition even (repeat 0)) is undefined.
|
|
Sublist predicates
|
|
|
The isPrefix function returns True if the first argument is
a prefix of the second.
|
|
Indexing streams
|
|
|
xs !! n returns the element of the stream xs at index
n. Note that the head of the stream has index 0.
Beware: passing a negative integer as the first argument will cause
an error.
|
|
Zipping and unzipping streams
|
|
|
The zip function takes two streams and returns a list of
corresponding pairs.
|
|
|
The zipWith function generalizes zip. Rather than tupling
the functions, the elements are combined using the function
passed as the first argument to zipWith.
|
|
|
The unzip function is the inverse of the zip function.
|
|
Functions on streams of characters
|
|
|
The words function breaks a stream of characters into a
stream of words, which were delimited by white space.
Beware: if the stream of characters xs does not contain white
space, accessing the tail of words xs will loop.
|
|
|
The unwords function is an inverse operation to words. It
joins words with separating spaces.
|
|
|
The lines function breaks a stream of characters into a list
of strings at newline characters. The resulting strings do not
contain newlines.
Beware: if the stream of characters xs does not contain
newline characters, accessing the tail of lines xs will loop.
|
|
|
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
|
|
|
The listToStream converts an infinite list to a
stream.
Beware: Passing a finite list, will cause an error.
|
|
|
The streamToList converts a stream into an infinite list.
|
|
Produced by Haddock version 2.4.2 |