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 tconst xis the infinite sequence where every element isx.
val cons : 'a -> 'a t -> 'a tcons x xsis the sequence of the elementxfollowed by the elemnts ofxs.
val iterate : ('a -> 'a) -> 'a -> 'a tGiven an endofunction
fand a starting pointx,iterate f xis the infinite sequence⟨x; f x; f (f x); f (f (f x)); ...⟩of iterated applications offstarting from the pointx.
val iterate_while : ('a -> 'a option) -> 'a -> 'a tGiven a partial endofunction
f : 'a -> 'acompleted asf' : 'a -> 'a option, and a starting pointx,iterate_while f' xis the finite or infinite sequence⟨x; f x; f (f x); f (f (f x)); ...⟩, terminated wherefwould have been applied outside its domain.
val memoize : 'a t -> 'a tmemoizeis 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 -> 'bfold f ⟨x₁; ...; xₙ⟩is the compositionf xₙ ∘ ... ∘ f x₁.
val find : ('a -> bool) -> 'a t -> 'a optionfind f xsis the firstxinxs, if any, such thatf xholds.
val length : 'a t -> intlength xsis the number of elements inxs.
val count : ('a -> bool) -> 'a t -> intcount f xsis the number of elementsxinxsfor whichf xholds.
val for_all : ('a -> bool) -> 'a t -> boolfor_all f xsis true ifff xis true for allxinxs.
val exists : ('a -> bool) -> 'a t -> boolexists f xsis true ifff xis true for somexinxs.
Slicing
val skip_upto : int -> 'a t -> 'a tskip_upto n xsis the subsequence following the firstnelements ofxs, or the empty sequence ifxshas less thannelements.- raises Invalid_argument
if the first argument is negative.
val skip_while : ('a -> bool) -> 'a t -> 'a tskip_while f xsis subsequence following the longest prefix ofxsfor which each elementxvalidatesf x.
Transformation
val differentiate : ('a -> 'a -> 'b) -> 'a -> 'a t -> 'b tdifferentiate f x₀ ⟨x₁; ...; xₙ⟩is the sequence⟨f x₀ x₁; ...; f xₙ₋₁ xₙ⟩.
val integrate : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b tintegrate f ⟨v₁; ...; vₙ⟩ x₀is⟨x₁; ...; xₙ⟩wherexᵢ = f vᵢ xᵢ₋₁fori ∈ {1, ..., n}.This can be seen an opposite operation of
differentiatein the sense that given two functinosf : α -> β -> βandf' : β -> β -> αsuch thatf' x (f v x) = vfor allx : βandv : α, thendifferentiate f' x₀ (integrate f ⟨v₁; ...; vₙ⟩ x₀) = ⟨v₁; ...; vₙ⟩.
Combination
Operator Shortcuts
module Infix : sig ... end