Module Interval.Make

Parameters

module T : ARITH

Signature

type t = T.t * T.t

The type of T.t intervals.

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

(v lo hi) is the interval (lo,hi).

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 (+) : (T.t * T.t) -> (T.t * T.t) -> T.t * T.t

( + ) is interval addition.

val (-) : (T.t * T.t) -> (T.t * T.t) -> T.t * T.t

( - ) is interval subtraction.

val (*) : (T.t * T.t) -> (T.t * T.t) -> T.t

( * ) is interval multiplication.

val (/) : (T.t * T.t) -> (T.t * T.t) -> T.t

( / ) is interval division.

val between : ('a * 'a) -> 'a -> bool

(between (lo,hi) n) is true iff n is contained in the open interval (lo,hi).

  • (between (lo,hi) n) = (lo < n && n < hi)
val contains : ('a * 'a) -> 'a -> bool

(contains (lo,hi) n) is true iff n is contained in the closed interval [lo,hi].

  • (contains (lo,hi) n) = (lo <= n && n <= hi)
val inbounds : ('a * 'a) -> 'a -> bool

(inbounds (lo,hi) n) is true iff n is contained in the half-open interval [lo,hi).

This is suitable for bounds-testing 0-indexed data structures like String's and Vector's.

  • (inbounds (lo,hi) n) = (lo <= n && n < hi)

Example:

  • String.(if inbounds (0,length str) i then str.[i] else '?')

Functionals

val foldil : ('a -> int -> 'a) -> 'a -> (int * int) -> 'a

foldil f i (a,z): folds f left-associatively across the closed interval [a,z] (tail-recursive).

i is the initial accumulator value.

Examples:

  • (foldil ( * ) 1 (1,6)) = ((((((1*1)*2)*3)*4)*5)*6) = 720
val foldir : (T.t -> 'a -> 'a) -> 'a -> (T.t * T.t) -> 'a

foldir f i (a,z): folds f right-associatively across the closed interval [a,z] (not tail-recursive)

i is the initial accumulator value.

Examples:

  • (foldir ( + ) 0 (1,6)) = (0+(1+(2+(3+(4+(5+6)))))) = 21
  • (foldir cons [] (1,6)) = [1; 2; 3; 4; 5; 6]
val foreach : (T.t -> 'a) -> (T.t * T.t) -> unit

foreach f (a,z): iterates f for side-effect across the closed interval [a,z] (tail-recursive)

(let r = ref 1 in foreach (( *:= ) r) (1,6); !r) = 720 
val to_string : ?left:string -> ?sep:string -> ?right:string -> ('a -> string) -> ('a * 'a) -> string

(to_string ?left ?sep ?right f (a,z)) returns a string representation of the interval (a,z).

The optional parameters are interpreted as for Pair.to_string.

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

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

val random : unit -> T.t * T.t

(random ()) is a random t.