1 | |
---|
2 | let rec max_list = function |
---|
3 | | [] -> raise (Invalid_argument "MiscPottier.max_list") |
---|
4 | | [a] -> a |
---|
5 | | a :: l -> max a (max_list l) |
---|
6 | |
---|
7 | let pow a b = |
---|
8 | if b < 0 then raise (Invalid_argument "MiscPottier.pow2") |
---|
9 | else |
---|
10 | let rec aux = function |
---|
11 | | 0 -> 1 |
---|
12 | | i -> a * aux (i-1) in |
---|
13 | aux b |
---|
14 | |
---|
15 | let rec make a n = |
---|
16 | if n = 0 then [] |
---|
17 | else a :: (make a (n-1)) |
---|
18 | |
---|
19 | let index_of x = |
---|
20 | let rec aux i = function |
---|
21 | | [] -> raise Not_found |
---|
22 | | y :: l -> if y = x then i else aux (i+1) l |
---|
23 | in |
---|
24 | aux 0 |
---|
25 | |
---|
26 | let rec remove_n_first n = |
---|
27 | let rec aux i = function |
---|
28 | | [] -> [] |
---|
29 | | l when i = n -> l |
---|
30 | | _ :: l -> aux (i+1) l in |
---|
31 | aux 0 |
---|
32 | |
---|
33 | let foldi_from_until n m f a l = |
---|
34 | let rec aux i res = function |
---|
35 | | [] -> res |
---|
36 | | _ when i >= m -> res |
---|
37 | | e :: l -> aux (i+1) (f i res e) l in |
---|
38 | aux 0 a (remove_n_first n l) |
---|
39 | |
---|
40 | let foldi_from n f a l = foldi_from_until n (List.length l) f a l |
---|
41 | |
---|
42 | let foldi_until m f a l = foldi_from_until 0 m f a l |
---|
43 | |
---|
44 | let foldi f a l = foldi_from_until 0 (List.length l) f a l |
---|
45 | |
---|
46 | let iteri f l = |
---|
47 | let rec aux i = function |
---|
48 | | [] -> () |
---|
49 | | e :: l -> f i e ; aux (i+1) l |
---|
50 | in |
---|
51 | aux 0 l |
---|
52 | |
---|
53 | let mapi f l = |
---|
54 | let rec aux i = function |
---|
55 | | [] -> [] |
---|
56 | | e :: l -> (f i e) :: (aux (i+1) l) |
---|
57 | in |
---|
58 | aux 0 l |
---|
59 | |
---|
60 | let rec last = function |
---|
61 | | [] -> raise Not_found |
---|
62 | | [a] -> a |
---|
63 | | _ :: l -> last l |
---|
64 | |
---|
65 | (* [split a i] splits the list a in two lists: one with the elements |
---|
66 | up until the [i]th (exclusive) and one with the rest. *) |
---|
67 | |
---|
68 | let rec split l i = |
---|
69 | if i = 0 then ([], l) |
---|
70 | else |
---|
71 | let (l1, l2) = split (List.tl l) (i-1) in |
---|
72 | ((List.hd l) :: l1, l2) |
---|
73 | |
---|
74 | (* [split_last l] returns the list [l] without its last element and its last |
---|
75 | element. Raises Invalid_argument "MiscPottier.split_last" if the list is |
---|
76 | empty. *) |
---|
77 | |
---|
78 | let split_last l = match split l ((List.length l) - 1) with |
---|
79 | | l', last :: _ -> (l', last) |
---|
80 | | _ -> raise (Invalid_argument "MiscPottier.split_last") |
---|
81 | |
---|
82 | let rec update_list_assoc a b = function |
---|
83 | | [] -> [] |
---|
84 | | (a', b') :: l -> |
---|
85 | if a' = a then (a, b) :: l else (a', b') :: (update_list_assoc a b l) |
---|
86 | |
---|
87 | (* Pasted from Pottier's PP compiler *) |
---|
88 | |
---|
89 | let rec combine xs1 xs2 = |
---|
90 | match xs1, xs2 with |
---|
91 | | [], _ |
---|
92 | | _, [] -> |
---|
93 | [] |
---|
94 | | x1 :: xs1, x2 :: xs2 -> |
---|
95 | (x1, x2) :: combine xs1 xs2 |
---|
96 | |
---|
97 | let rec subtract xs1 xs2 = |
---|
98 | match xs1, xs2 with |
---|
99 | | [], _ -> |
---|
100 | [] |
---|
101 | | _, [] -> |
---|
102 | xs1 |
---|
103 | | _ :: xs1, _ :: xs2 -> |
---|
104 | subtract xs1 xs2 |
---|
105 | |
---|
106 | let mirror l = |
---|
107 | List.map (fun (x, y) -> (y, x)) l |
---|
108 | |
---|
109 | let length l = |
---|
110 | Int32.of_int (List.length l) |
---|
111 | |
---|
112 | let rec prefix k l = |
---|
113 | match k, l with |
---|
114 | | 0, _ |
---|
115 | | _, [] -> |
---|
116 | [] |
---|
117 | | _, x :: xs -> |
---|
118 | x :: prefix (k - 1) xs |
---|
119 | |
---|
120 | let memoize f = |
---|
121 | let table = Hashtbl.create 131 in |
---|
122 | fun key -> |
---|
123 | try |
---|
124 | Hashtbl.find table key |
---|
125 | with Not_found -> |
---|
126 | let data = f key in |
---|
127 | Hashtbl.add table key data; |
---|
128 | data |
---|
129 | |
---|
130 | let filter_map filter map = |
---|
131 | let rec aux = function |
---|
132 | | [] -> [] |
---|
133 | | e :: l -> (if filter e then [map e] else []) @ (aux l) |
---|
134 | in |
---|
135 | aux |
---|
136 | |
---|
137 | let string_of_list sep f = |
---|
138 | let rec aux = function |
---|
139 | | [] -> "" |
---|
140 | | [e] -> f e |
---|
141 | | e :: l -> (f e) ^ sep ^ (aux l) |
---|
142 | in |
---|
143 | aux |
---|