diff options
Diffstat (limited to 'libs')
-rw-r--r-- | libs/util.ml | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/libs/util.ml b/libs/util.ml index 01b0c5c..443fb1e 100644 --- a/libs/util.ml +++ b/libs/util.ml @@ -1,3 +1,21 @@ +(* Small things *) + +let ord_couple (a, b) = if a < b then a, b else b, a + +(* uniq_sorted : 'a list -> 'a list *) +let rec uniq_sorted = function + | [] -> [] + | [a] -> [a] + | a::b::q when a = b -> uniq_sorted (b::q) + | a::r -> a::(uniq_sorted r) + +(* list_fold_op : ('a -> 'a -> 'a) -> 'a list -> 'a *) +let rec list_fold_op op = function + | [] -> invalid_arg "list_fold_opt on empty list" + | [a] -> a + | x::q -> op x (list_fold_op op q) + + (* Either type *) type ('a, 'b) either = @@ -47,24 +65,15 @@ let rec fix equal f s = let (@@) f x = f x -let print_list x l = - Format.printf "%s: " x; +let print_list f sep fmt l = let rec aux = function | [] -> () - | [a] -> Format.printf "%s" a - | p::q -> Format.printf "%s, " p; aux q + | [a] -> f fmt a + | a::b -> f fmt a; Format.fprintf fmt "%s@," sep; aux b in - Format.printf "["; aux l; Format.printf "]@." + aux l let uid = let c = ref 0 in fun () -> c := !c + 1; string_of_int !c -(* On lists *) - -(* list_fold_op : ('a -> 'a -> 'a) -> 'a list -> 'a *) -let rec list_fold_op op = function - | [] -> invalid_arg "list_fold_opt on empty list" - | [a] -> a - | x::q -> op x (list_fold_op op q) - |