New misc functions, renamed make_list_compare to list_compare.

New functions fold_right_i and option_compare.
This commit is contained in:
Adrien Guatto 2010-09-30 19:24:41 +02:00
parent 3d860c3624
commit 1fd2f374ff
3 changed files with 20 additions and 6 deletions

View file

@ -52,7 +52,7 @@ struct
(** two instances are equal if the desc of keys are equal *)
let compare_instances =
let compare se1 se2 = compare se1.se_desc se2.se_desc in
Misc.make_list_compare compare
Misc.list_compare compare
module S = (** Instances set *)
Set.Make(

View file

@ -65,7 +65,7 @@ let rec split_last = function
let remove x l =
List.filter (fun y -> x <> y) l
let make_list_compare c l1 l2 =
let list_compare c l1 l2 =
let rec aux l1 l2 = match (l1, l2) with
| (h1::t1, h2::t2) ->
let result = c h1 h2 in
@ -75,6 +75,12 @@ let make_list_compare c l1 l2 =
| ([], _ ) -> -1
in aux l1 l2
let option_compare f ox1 ox2 = match ox1, ox2 with
| None, None -> 0
| Some x1, Some x2 -> f x1 x2
| None, _ -> -1
| _, None -> 1
let is_empty = function
| [] -> true
| _ -> false
@ -144,6 +150,12 @@ let mapi3 f l1 l2 l3 =
in
aux 0 l1 l2 l3
let fold_righti f l acc =
let rec aux i l acc = match l with
| [] -> acc
| h :: l -> f i h (aux (i + 1) l acc) in
aux 0 l acc
(* Functions to decompose a list into a tuple *)
let _arity_error i l =
Format.eprintf "Internal compiler error: \

View file

@ -49,9 +49,11 @@ val memd_assoc : 'b -> ('a * 'b) list -> bool
(** Same as List.assoc but searching for a data and returning the key. *)
val assocd : 'b -> ('a * 'b) list -> 'a
(** [make_compare c] generates the lexicographical compare function on lists
induced by [c] *)
val make_list_compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int
(** [list_compare c l1 l2] compares the lists [l1] and [l2] according to
lexicographical order induced by [c]. *)
val list_compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int
val option_compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
(** Mapfold *)
val mapfold: ('a -> 'b -> 'c * 'a) -> 'a -> 'b list -> 'c list * 'a
@ -65,6 +67,7 @@ val mapi: (int -> 'a -> 'b) -> 'a list -> 'b list
val mapi2: (int -> 'a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val mapi3: (int -> 'a -> 'b -> 'c -> 'd) ->
'a list -> 'b list -> 'c list -> 'd list
val fold_righti : (int -> 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
(** Functions to decompose a list into a tuple *)
val assert_empty : 'a list -> unit
@ -73,4 +76,3 @@ val assert_1min : 'a list -> 'a * 'a list
val assert_2 : 'a list -> 'a * 'a
val assert_2min : 'a list -> 'a * 'a * 'a list
val assert_3 : 'a list -> 'a * 'a * 'a