Module Prelude.Pair

Useful functions on 2-tuples.

val of_list : 'a list -> 'a * 'a

(of_list [a;b]) is the pair (a,b); a list of any length other than 2 raises invalid_arg _.

val to_list : ('a * 'a) -> 'a list

(to_list (a,b)) is [a; b].

val dup : 'a -> 'a * 'a

(dup a) is (a,a).

val up : 'a -> 'b -> 'a * 'b

(up a b) is (a,b).

val swap : ('a * 'b) -> 'b * 'a

(swap (a,b)) is (b,a)

val map : ('a -> 'b) -> ('a * 'a) -> 'b * 'b

(map f (a,b)) is (f a, f b): applies the same function to each element of the pair.

val iter : ('a -> 'b) -> ('a * 'a) -> 'b

(iter f (a,b)) is (f a, f b): applies the same function to each element of the pair, for side-effect.

val onleft : ('a -> 'b) -> ('a * 'c) -> 'b * 'c

(onleft f (a,b)) is (f a, b).

val onright : ('a -> 'b) -> ('c * 'a) -> 'c * 'b

(onright f (a,b)) is (a, f b).

val cross : ('a -> 'b) -> ('c -> 'd) -> ('a * 'c) -> 'b * 'd

(cross f g (a,b)) is (f a, g b): applies two different functions, one to each element of the pair.

(cross succ pred (1,1)) = (2,0) 
val (&&&) : ('a -> 'b) -> ('c -> 'd) -> ('a * 'c) -> 'b * 'd

(&&&) is Pair.cross.

val diag : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c

(diag f g x) is (f x, g x): applies two different functions to the same value, returning a pair of results.

(diag succ pred x) = (cross succ pred (x,x)) 
val (***) : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c

( *** ) is Pair.diag.

val cartesian_product : ('a * 'a) -> ('b * 'b) -> ('a * 'b) list

(cartesian_product x y) is the Cartesian product of the two pairs.

cartesian_product (a,b) (c,d) = [a,c; a,d; b,c; b,d] 
val (*) : ('a * 'a) -> ('b * 'b) -> ('a * 'b) list
val to_string : ?left:string -> ?sep:string -> ?right:string -> ('a -> string) -> ('b -> string) -> ('a * 'b) -> string

(to_string ?left ?sep ?right f g (a,b)) returns a string representation of (a,b).

f is used for the string representation of a and g for b.

The representation of the two values is inserted between ?left (default: "(") and ?right (default: ")"), separated by ?sep (default: ", ").

Example: (to_string string_of_int id (12,"foo")) = {|(12, "foo")|}

val print : ?left:string -> ?sep:string -> ?right:string -> ('a -> string) -> ('b -> string) -> ('a * 'b) -> unit

(print ?left ?sep ?right f g) is (to_string ?left ?sep ?right f g >> print_endline).

val random : (unit -> 'a) -> (unit -> 'b) -> unit -> 'a * 'b

(random r1 r2) is a random pair of values (v1,v2) s.t. (v1 = r1 ()) and (v2 = r2 ()).

Ops

module Ops : sig ... end

Infix and prefix operators.