2 let rec map3 f al bl cl =
3 let f' ((a, b), c) = f a b c in
4 List.map f' (List.combine (List.combine al bl) cl)
6 let rec max_list = function
7 | [] -> raise (Invalid_argument "MiscPottier.max_list")
9 | a :: l -> max a (max_list l)
11 let rec reduce l1 l2 = match l1, l2 with
12 | [], _ -> (([], []), ([], l2))
13 | _, [] -> (([], l1), ([], []))
15 let ((common1, rest1), (common2, rest2)) = reduce l1 l2 in
16 ((a :: common1, rest1), (b :: common2, rest2))
19 if b < 0 then raise (Invalid_argument "MiscPottier.pow2")
21 let rec aux = function
23 | i -> a * aux (i-1) in
28 else a :: (make a (n-1))
31 let rec aux i = function
32 | [] -> raise Not_found
33 | y :: l -> if y = x then i else aux (i+1) l
37 let rec remove_n_first n =
38 let rec aux i = function
41 | _ :: l -> aux (i+1) l in
44 let foldi_from_until n m f a l =
45 let rec aux i res = function
47 | _ when i >= m -> res
48 | e :: l -> aux (i+1) (f i res e) l in
49 aux 0 a (remove_n_first n l)
51 let foldi_from n f a l = foldi_from_until n (List.length l) f a l
53 let foldi_until m f a l = foldi_from_until 0 m f a l
55 let foldi f a l = foldi_from_until 0 (List.length l) f a l
58 let f i res e' = if e' = e then Some i else res in
59 match foldi f None l with
60 | None -> raise Not_found
64 let rec aux i = function
66 | e :: l -> f i e ; aux (i+1) l
71 let rec aux i = function
73 | e :: l -> (f i e) :: (aux (i+1) l)
77 let rec last = function
78 | [] -> raise Not_found
82 (* [split a i] splits the list a in two lists: one with the elements
83 up until the [i]th (exclusive) and one with the rest. *)
88 let (l1, l2) = split (List.tl l) (i-1) in
89 ((List.hd l) :: l1, l2)
91 (* [split_last l] returns the list [l] without its last element and its last
92 element. Raises Invalid_argument "MiscPottier.split_last" if the list is
95 let split_last l = match split l ((List.length l) - 1) with
96 | l', last :: _ -> (l', last)
97 | _ -> raise (Invalid_argument "MiscPottier.split_last")
99 let rec update_list_assoc a b = function
102 if a' = a then (a, b) :: l else (a', b') :: (update_list_assoc a b l)
104 (* Pasted from Pottier's PP compiler *)
106 let rec combine xs1 xs2 =
111 | x1 :: xs1, x2 :: xs2 ->
112 (x1, x2) :: combine xs1 xs2
114 let rec subtract xs1 xs2 =
120 | _ :: xs1, _ :: xs2 ->
124 List.map (fun (x, y) -> (y, x)) l
127 Int32.of_int (List.length l)
135 x :: prefix (k - 1) xs
138 let table = Hashtbl.create 131 in
141 Hashtbl.find table key
144 Hashtbl.add table key data;
147 let filter_map filter map =
148 let rec aux = function
150 | e :: l -> (if filter e then [map e] else []) @ (aux l)
154 let string_of_list sep f =
155 let rec aux = function
158 | e :: l -> (f e) ^ sep ^ (aux l)