35 |
if e = ele then remove_rec ele list tail |
if e = ele then remove_rec ele list tail |
36 |
else |
else |
37 |
remove_rec ele list (e :: tail) |
remove_rec ele list (e :: tail) |
38 |
|
|
39 |
let remove ele list = |
let remove ele list = |
40 |
remove_rec ele list [] |
remove_rec ele list [] |
41 |
|
|
43 |
match list with |
match list with |
44 |
[] -> List.rev tail |
[] -> List.rev tail |
45 |
| e :: list -> |
| e :: list -> |
46 |
if e = ele then |
if e = ele then |
47 |
List.rev_append tail list |
List.rev_append tail list |
48 |
else |
else |
49 |
remove_one_rec ele list (e :: tail) |
remove_one_rec ele list (e :: tail) |
50 |
|
|
51 |
let remove_one ele list = |
let remove_one ele list = |
52 |
remove_rec ele list [] |
remove_rec ele list [] |
53 |
|
|
54 |
let rec removeq_first ele list = |
let rec removeq_first ele list = |
55 |
match list with |
match list with |
56 |
e :: tail when e == ele -> tail |
e :: tail when e == ele -> tail |
68 |
(0,_) | (_, []) -> List.rev r, list |
(0,_) | (_, []) -> List.rev r, list |
69 |
| _, x :: tail -> |
| _, x :: tail -> |
70 |
cut_rec (n-1) tail (x :: r) |
cut_rec (n-1) tail (x :: r) |
71 |
|
|
72 |
let cut n list = |
let cut n list = |
73 |
if n < 0 then failwith "List2.sub: invalid parameter"; |
if n < 0 then failwith "List2.sub: invalid parameter"; |
74 |
cut_rec n list [] |
cut_rec n list [] |
76 |
let tail_map f list = |
let tail_map f list = |
77 |
List.rev (List.rev_map f list) |
List.rev (List.rev_map f list) |
78 |
|
|
79 |
|
|
80 |
let rec assoc_inv x = function |
let rec assoc_inv x = function |
81 |
[] -> raise Not_found |
[] -> raise Not_found |
82 |
| (a,b)::l -> if b = x then a else assoc_inv x l |
| (a,b)::l -> if b = x then a else assoc_inv x l |
83 |
|
|
84 |
let safe_iter f list = |
let safe_iter f list = |
85 |
List.iter (fun v -> try f v with _ -> ()) list |
List.iter (fun v -> try f v with _ -> ()) list |
86 |
|
|
87 |
let min list = |
let min list = |
88 |
let rec iter m tail = |
let rec iter m tail = |
89 |
match tail with |
match tail with |
90 |
[] -> m |
[] -> m |
91 |
| m' :: tail -> |
| m' :: tail -> |
92 |
iter (min m' m) tail |
iter (min m' m) tail |
93 |
in |
in |
94 |
match list with |
match list with |
95 |
[] -> raise Not_found |
[] -> raise Not_found |
96 |
| m :: tail -> iter m tail |
| m :: tail -> iter m tail |
97 |
|
|
98 |
let max list = |
let max list = |
99 |
let rec iter m tail = |
let rec iter m tail = |
100 |
match tail with |
match tail with |
101 |
[] -> m |
[] -> m |
102 |
| m' :: tail -> |
| m' :: tail -> |
103 |
iter (max m' m) tail |
iter (max m' m) tail |
104 |
in |
in |
105 |
match list with |
match list with |
106 |
[] -> raise Not_found |
[] -> raise Not_found |
|
| m :: tail -> iter m tail |
|
107 |
|
| m :: tail -> iter m tail |