1 (* Function composition *)
2 let (++) f g x = f (g x);;
5 let rec aux n = function
6 | [] -> raise Not_found
7 | x::_ when p x -> n, x
8 | _::xs -> aux (n+1) xs
12 let option_map f = function
14 | Some x -> Some (f x)
23 | Some _ as res -> res
25 let rec index_of ?(eq=(=)) x =
28 | y::_ when eq x y -> 0
29 | _::l -> 1 + index_of ~eq x l
31 let index_of_opt ?eq l t =
33 Some (index_of ?eq t l)
38 let rec filter_map f =
43 None -> filter_map f tl
44 | Some x -> x::filter_map f tl
47 (* the input must be sorted *)
48 let rec first_duplicate =
52 | x::y::_ when x=y -> Some x
53 | _::tl -> first_duplicate tl
55 (* the input must be sorted
56 output: list of non duplicates; list of duplicates *)
57 let rec split_duplicates =
62 let nondup,dup = split_duplicates tl in
64 List.filter ((<>) x) nondup, x::dup
68 (* Non c'e' nella vecchia versione di OCaml :( *)
69 let uniq ?(compare=compare) =
70 let rec aux = function
73 | t1 :: (t2 :: _ as ts) ->
74 if compare t1 t2 = 0 then aux ts else t1 :: (aux ts)
77 let sort_uniq ?(compare=compare) l = uniq ~compare (List.sort compare l)
79 let rec list_cut = function
81 | n, x::xs -> let a, b = list_cut (n-1,xs) in x::a, b
85 let concat_map f l = List.concat (List.map f l);;
89 | [] -> assert (n = 0); []
91 | x::xs -> x::(take (n-1) xs)
99 then "`" ^ string_of_int v
100 else String.make 1 (char_of_int (v + int_of_char 'a'))
103 let var_of_string s =
104 if String.length s <> 1 then (
105 if s.[0] = '`' then int_of_string (String.sub s 1 (-1 + String.length s)) else assert false
106 ) else int_of_char s.[0] - int_of_char 'a'
111 else if n >= List.length l then "x" ^ string_of_int (List.length l - n - 1) else List.nth l n