module Streams: sig .. end
The Streams module is used to manipulate infinite sequences.
type +'a stream
The
'a stream type is used to represent infinite sequences with
elements of type
'a. The
+ in the type signature means that
this type is
covariant in
'a. For practical purposes, this lets the interpreter
perform more advanced type inference.
val fork : ('a -> 'b) * ('a -> 'c) -> 'a -> 'b * 'c
fork is a combinator for executing two functions on the same
input and returning the results. More precisely,
fork (f,g) x = (f x, g x)
val take : int -> 'a stream -> 'a list
take n s returns the first n elements of the stream s in a
list.
val unfold : ('a -> 'b * 'a) -> 'a -> 'b stream
unfold f s takes the seed
s and a function
f that produces a
tuple containing two values:
- a value to be placed into the
stream, and
- the next seed.
The function
f is applied
successively to generate all of the values of the stream.
val univ : ('a -> 'b) * ('a -> 'a) -> 'a -> 'b stream
The
univ function is the composition of
unfold and
fork. Consequently,
univ (f,g) s specifies a function
f that
generates the next element of the stream and
g that generates
the next seed. The function
univ has a
universal
property.
val hd : 'a stream -> 'a
hd returns the first element of a stream.
val tl : 'a stream -> 'a stream
tl returns the stream without its first element
val repeat : 'a -> 'a stream
repeat x returns a stream that has every element equal to
x.
val map : ('a -> 'b) -> 'a stream -> 'b stream
map f s creates a new stream by applying f to each element of
s. Returns in O(1) time.
val diag : 'a stream stream -> 'a stream
diag ss takes a stream of streams and returns a stream of the
diagonal elements. That is, the nth element of the output stream is
the nth element of the nth stream in the input.
val suffixes : 'a stream -> 'a stream stream
suffixes s returns a stream whose nth element is the substream
of s starting at the nth element of s.
val interleave : 'a stream -> 'a stream -> 'a stream
interleave s t returns a stream with the elements of s and t
in alternating order.
val fibs : int stream
fibs is a stream whose nth element is the nth fibonacci
number.
val pi : float stream
pi is a stream that contains a sequence of approximations that
converge to the real number pi. The elements of this stream are
the partial sums of
this series.
val look_and_say : int list stream