Module Prime_seq
Amendment to the standard library Seq
structure. This does not include the original functions, see Unprime_seq
for a full replacement.
In the following, sequences are denoted with angle brackets as ⟨x₁; ...;
xₙ⟩
. Despite this notation, we allow for unbounded sequences as long as inspecting a finite subsequence is sufficient to carry out the computation. The implied limit n → ∞
is merely formal, and serves to avoid duplicating formulas for finite and infinite cases.
Construction
val const : 'a -> 'a t
const x
is the infinite sequence where every element isx
.
val cons : 'a -> 'a t -> 'a t
cons x xs
is the sequence of the elementx
followed by the elemnts ofxs
.
val iterate : ('a -> 'a) -> 'a -> 'a t
Given an endofunction
f
and a starting pointx
,iterate f x
is the infinite sequence⟨x; f x; f (f x); f (f (f x)); ...⟩
of iterated applications off
starting from the pointx
.
val iterate_while : ('a -> 'a option) -> 'a -> 'a t
Given a partial endofunction
f : 'a -> 'a
completed asf' : 'a -> 'a option
, and a starting pointx
,iterate_while f' x
is the finite or infinite sequence⟨x; f x; f (f x); f (f (f x)); ...⟩
, terminated wheref
would have been applied outside its domain.
val memoize : 'a t -> 'a t
memoize
is the identity function on sequences, except that it ensures that each step in the computation involved in the original sequence is performed only once, on demand. This will incur a memory overhead proportional to the distance from the earliest still retained node to the lastest visited node.
Consumption
val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold f ⟨x₁; ...; xₙ⟩
is the compositionf xₙ ∘ ... ∘ f x₁
.
val find : ('a -> bool) -> 'a t -> 'a option
find f xs
is the firstx
inxs
, if any, such thatf x
holds.
val length : 'a t -> int
length xs
is the number of elements inxs
.
val count : ('a -> bool) -> 'a t -> int
count f xs
is the number of elementsx
inxs
for whichf x
holds.
val for_all : ('a -> bool) -> 'a t -> bool
for_all f xs
is true ifff x
is true for allx
inxs
.
val exists : ('a -> bool) -> 'a t -> bool
exists f xs
is true ifff x
is true for somex
inxs
.
Slicing
val skip_upto : int -> 'a t -> 'a t
skip_upto n xs
is the subsequence following the firstn
elements ofxs
, or the empty sequence ifxs
has less thann
elements.- raises Invalid_argument
if the first argument is negative.
val skip_while : ('a -> bool) -> 'a t -> 'a t
skip_while f xs
is subsequence following the longest prefix ofxs
for which each elementx
validatesf x
.
Transformation
val differentiate : ('a -> 'a -> 'b) -> 'a -> 'a t -> 'b t
differentiate f x₀ ⟨x₁; ...; xₙ⟩
is the sequence⟨f x₀ x₁; ...; f xₙ₋₁ xₙ⟩
.
val integrate : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b t
integrate f ⟨v₁; ...; vₙ⟩ x₀
is⟨x₁; ...; xₙ⟩
wherexᵢ = f vᵢ xᵢ₋₁
fori ∈ {1, ..., n}
.This can be seen an opposite operation of
differentiate
in the sense that given two functinosf : α -> β -> β
andf' : β -> β -> α
such thatf' x (f v x) = v
for allx : β
andv : α
, thendifferentiate f' x₀ (integrate f ⟨v₁; ...; vₙ⟩ x₀) = ⟨v₁; ...; vₙ⟩
.
Combination
Operator Shortcuts
module Infix : sig ... end