-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliblist.ml
77 lines (59 loc) · 1.58 KB
/
liblist.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(* Yet another List Library *)
open List;;
exception SearchList;;
exception NoMapList;;
let pos_list m pos l = try nth l (pos-m) with _ -> raise SearchList
;;
let cut_list m pos l =
let rec cut_aux pos = function
[] -> []
| t::r -> if pos = m then r
else cut_aux (pos-1) r
in
if pos < m then l
else cut_aux pos l
;;
let rec search_list n fun_eq = function
[] -> raise SearchList
| x::r -> if fun_eq x then n
else search_list (n+1) fun_eq r
;;
let rec get_list fun_eq = function
[] -> raise (SearchList)
| x::r -> if fun_eq x then x
else get_list fun_eq r
;;
let rec elim_list fun_eq = function
[] -> []
| x::r -> if fun_eq x then r
else x::elim_list fun_eq r
;;
let search_elim_list fun_eq l =
let rec search_elim_i li = function
[] -> raise (SearchList)
| x::r -> if fun_eq x then (x,li@r)
else search_elim_i (li@[x]) r in
search_elim_i [] l
;;
let rec print_list print_e empty se pi pd = function
[] -> print_string empty
| x::y -> print_string pi;
print_e x;
if y <> [] then (
print_string se;
print_list print_e empty se "" "" y);
print_string pd;;
let rec merge_list = fun
p0 p1 -> match (p0,p1) with ((x::y), (z::w)) -> (x,z)::merge_list y w
| (_, _) -> []
;;
let rec fmerge_list f g = fun
p0 p1 -> match (p0,p1) with ((x::y), (z::w)) -> (f x, g z)::fmerge_list f g y w
| (_, _) -> []
;;
let rec insert_sorted a f = function
[] -> [a]
| (x::y) as l ->
if f a x then a::l
else x::insert_sorted a f y
;;