Module Cf_seq


module Cf_seq: sig .. end
Lazily-evaluated sequences (functional streams).


Overview


This module implements a functional stream type. It's like the built-in list type, but the tail of every element in the list is lazily evaluated. This means it is possible to represent sequences of infinite length, and define functions that operate on sequences in a functional, rather than imperative mode. Many of the other modules in the cf library make extensive use of this type.

The functions for manipulating static lists in the standard List library all have equivalents here in this module. Additionally, there are some convenient functions for converting structures with imperative interfaces into sequences that permit functional algorithms to be applied.

Types

type 'a t = 'a cell Lazy.t 
A lazily-evaluated sequence.

type 'a cell =
| P of 'a * 'a t (*Element of a sequence*)
| Z (*End of sequence*)

Exceptions

exception Empty
Operation not possible on an empty list.

Functions

val nil : 'a t
An empty sequence.
val head : 'a t -> 'a
Returns the first element in the sequence. Raises Empty if the sequence has no elements, i.e. if the sequence is Z.
val tail : 'a t -> 'a t
Discards the first element in the sequence and returns the sequence of remaining elements. Raises Empty if the sequence has no elements, i.e. if the sequence is Z.
val concat : 'a t -> 'a t -> 'a t
concat a b returns the sequence of all the elements in a followed by all the elements in b. Adds a constant cost to the evaluation of every element in the resulting sequence prior to the start of elements from b, so it may be worth considering the use of a Cf_deque object in place of a Cf_seq object to avoid cost explosion.
val flatten : 'a t t -> 'a t
flatten a returns the sequence of all the elements in the sequence of sequences by concatenating them.
val limit : ?x:exn -> int -> 'a t -> 'a t
limit n s returns the sequence of all the elements in s, up to n elements in number and no more. Raises Invalid_argument if n < 0. If ?x is provided, then the exception is raised if the sequence is evaluated past the limit.
val shift : int -> 'a t -> 'a t
shift n s returns the sequence of all the elements in s after the first n elements are discarded. Returns the empty sequence if s has fewer than n elements.
val sentinel : exn -> 'a t -> 'a t
sentinel x s returns a sequence identical to s except that x is raised by evaluating to the end. This is intended for use in incremental sequence processing.
val reverse : 'a t -> 'a list
reverse s evaluates the entire sequence and composes a list of the elements in reverse order. Tail recursive.
val length : 'a t -> int
Evaluates the entire sequence and returns the number elements.
val unfold : ('a -> ('b * 'a) option) -> 'a -> 'b t
unfold f a returns the sequence composed of the results of applying f according to the following rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the owing rule: the first application of f is with a as the argument; if the