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
13 let rec aux n = function
15 | x::xs when p x -> (n,x)::aux (n+1) xs
16 | _::xs -> aux (n+1) xs
20 let option_map f = function
22 | Some x -> Some (f x)
25 let option_get = function
27 | None -> failwith "option_get: None"
36 | Some _ as res -> res
38 let rec index_of ?(eq=(=)) x =
41 | y::_ when eq x y -> 0
42 | _::l -> 1 + index_of ~eq x l
44 let index_of_opt ?eq l t =
46 Some (index_of ?eq t l)
51 let rec filter_map f =
56 None -> filter_map f tl
57 | Some x -> x::filter_map f tl
60 (* the input must be sorted *)
61 let rec first_duplicate =
65 | x::y::_ when x=y -> Some x
66 | _::tl -> first_duplicate tl
68 (* the input must be sorted
69 output: list of non duplicates; list of duplicates *)
70 let rec split_duplicates =
75 let nondup,dup = split_duplicates tl in
77 List.filter ((<>) x) nondup, x::dup
81 (* Non c'e' nella vecchia versione di OCaml :( *)
82 let uniq ?(compare=compare) =
83 let rec aux = function
86 | t1 :: (t2 :: _ as ts) ->
87 if compare t1 t2 = 0 then aux ts else t1 :: (aux ts)
90 let sort_uniq ?(compare=compare) l = uniq ~compare (List.sort compare l)
92 let rec list_cut = function
94 | n, x::xs -> let a, b = list_cut (n-1,xs) in x::a, b
98 let concat_map f l = List.concat (List.map f l);;
102 | [] -> assert (n = 0); []
104 | x::xs -> x::(take (n-1) xs)
110 let string_of_var v =
112 then "`" ^ string_of_int v
113 else String.make 1 (char_of_int (v + int_of_char 'a'))
116 let var_of_string s =
117 if String.length s <> 1 then (
118 if s.[0] = '`' then int_of_string (String.sub s 1 (-1 + String.length s)) else assert false
119 ) else int_of_char s.[0] - int_of_char 'a'
124 else if n >= List.length l then "x" ^ string_of_int (List.length l - n - 1) else List.nth l n