package fmlib

  1. Overview
  2. Docs

Module Fmlib_std.ListSource

A thin wrapper around Stdlib.List which avoids throwing exceptions and with some additional monadic functions.

In case you need functions from the module List of the ocaml standard library, just use Stdlib.List

List Monad

Sourcetype 'a t = 'a list

A list of values of type 'a.

Sourceval return : 'a -> 'a t

return a makes a singleton list with the element a.

Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t

l >>= f applies the function f to all elements of the list l and concatenates all lists.

Sourceval (let*) : 'a t -> ('a -> 'b t) -> 'b t

let* a = m in f a is equivalent to m >>= f.

Sourceval (>=>) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t

f >=> g composes the two monadic functions f and g.

Sourceval (<*>) : ('a -> 'b) t -> 'a t -> 'b t

flst <*> lst is equivalent to flst >>= fun f -> map f lst i.e. it maps all functions contained in flst over the list lst and then concatenates the results.

Sourceval join : 'a list list -> 'a list

join is the same as concat.

Modified list functions

Sourceval find : ('a -> bool) -> 'a t -> 'a option

find p l finds an element e in the list l which satisfies p e.

List functions from Stdlib

Sourceval append : 'a list -> 'a list -> 'a list

append a b concatenate the lists a and b.

Synonym a @ b.

Sourceval concat : 'a list list -> 'a list

concat ll concatenates all lists contained in the list of lists ll.

Sourceval split : ('a * 'b) list -> 'a list * 'b list

Transform a list of pairs into a pair of lists.

Sourceval rev : 'a list -> 'a list

rev a reverses the list a.

Sourceval rev_append : 'a list -> 'a list -> 'a list

rev_append a b prepends the lists rev a in front of the list b.

Sourceval length : 'a t -> int

length l The length of the list l.

Sourceval filter : ('a -> bool) -> 'a t -> 'a t

filter p l Returns a list with all the elements of l which satisfy the predicate p.

Sourceval fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

fold_left f s l

Compute f (f ... (f a b1) ... bn-1) bn where l = [b1; ...; bn-1; bn]

Sourceval fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b

like fold_left, just iterate from right to left.

Sourceval map : ('a -> 'b) -> 'a list -> 'b list

map f l returns a list where all elements of l are mapped by the function f.

Sourceval mapi : (int -> 'a -> 'b) -> 'a list -> 'b list

mapi f l map all elements of the list l with the mapping function f which receives the index of the element (starting at zero) and the element.

Sourceval rev_map : ('a -> 'b) -> 'a list -> 'b list

rev_map f l The same as map (rev l). rev_map is tail recursive.

Sourceval for_all : ('a -> bool) -> 'a list -> bool

for_all p l checks, if all elements in the list l satisfy the predicate p.

Sourceval exists : ('a -> bool) -> 'a list -> bool

exists p l checks, if some element in the list l satisfies the predicate p.

Additional list functions

Sourceval split_head_tail : 'a t -> 'a * 'a t

split_head_tail l split the list in its head and tail part.

Precondition: The list is not empty.

Sourceval map_and_filter : ('a -> 'b option) -> 'a list -> 'b list

map_and_filter f list maps the list with f and removes the element for which f e = None.

Sourceval split_at : ('a -> bool) -> 'a t -> 'a t * 'a t

split_at p lst scans the list until it finds the first element satisfying p and returns the prefix and the remainder starting at the encountered element. If the second list is empty, then there is no element in the list satisfying p.

Sourceval transpose : 'a list list -> 'a list list

transpose list_of_rows returns the list of columns.

Preconditions:

  • The list of rows must not be empty.
  • All rows in the list of rows must not be empty and have the same length.

Example:

    transpose [ [1; 2; 3]; [4; 5; 6] ]
    =
    [ [1; 4]; [2; 5]; [3; 6] ]

Monadic list functions

Sourcemodule Monadic (M : Interfaces.MONAD) : sig ... end

Monadic list functions

OCaml

Innovation. Community. Security.