Module Prime_list
Amendment to the standard library List structure.
This structure does not include the original functions. For a full replacement include Unprime_list.
val count : ('a -> bool) -> 'a list -> intcount f xsis the number of elementsxofxsfor whichf xholds.
val search : ('a -> 'b option) -> 'a list -> 'b optionsearch f xsreturns the first element ofmap f xswhich is different fromNoneorNoneif all elements areNone. This is an alternative tofindwhich is easy to nest, e.g. the functionPrime_array.search (Prime_option.search (Prime_list.search f))returns the first non-Nonemapping offin an array of optional lists.
Comparison
Iteration
val fold : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'bfold f [x₁; …; xₙ]returns the compositionf xₙ ∘ ⋯ ∘ f x₁. This isfold_leftwith parameters reordered to make it more composable.
val fmap : ('a -> 'b option) -> 'a list -> 'b listval filter_map : ('a -> 'b option) -> 'a list -> 'b listfilter_map f xsis the list of allysuch thatf x = Some yfor somexinxs, and having the same order as the corresponding elemets ofxs. This provides an optimisation ofmap Option.get (filter ((<>) None) (map f xs)).
Iteration over Two Lists
val iter2t : ('a -> 'b -> unit) -> 'a list -> 'b list -> unititer2t f [x₁; …; xₙ] [y₁; …; yₘ]callsf x₁ y₁; …; f xₗ yₗin order, wherel = min n m. This is a truncating variant ofiter2from the standard library.
val map2t : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listmap2t f [x₁; …; xₙ] [y₁; …; yₘ]returns[f x₁ y₁; ⋯; f xₗ yₗ]wherel = min n m. Thetsuffix indicates truncation, otherwise this behaves likemap2from the standard library.
val rev_map2t : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listrev_map2t fis an optimisation ofrev ∘ map2t f.
Differential Iteration
Sublists
val drop : int -> 'a list -> 'a listdrop n xsreturns the suffix ofxsfollowing thenfirst elements. RaisesFailureifxsis shorter thannelements and Invalid_argument ifnis negative.
val take : int -> 'a list -> 'a listtake n xsreturns then-element prefix ofxs. RaisesFailureifxsis shorter thannelements and Invalid_argument ifnis negative.