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 n
th element of the output stream is
the n
th element of the n
th stream in the input.
val suffixes : 'a stream -> 'a stream stream
suffixes s
returns a stream whose n
th element is the substream
of s
starting at the n
th 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 n
th element is the n
th 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