forked from remirobert/list-ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.ml
134 lines (114 loc) · 3.44 KB
/
list.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
type 'a my_list =
| Item of ('a * 'a my_list)
| Empty
let rec length (list:'a my_list) =
match list with
| Empty -> 0
| Item (data, next) -> length next + 1
let rec display_content_list = (fun (list:'a my_list) ->
match list with
| Empty -> 0
| Item (data, next) -> print_string data;
print_endline "";
display_content_list next + 1)
let hd = (fun (list:'a my_list) ->
match list with
| Empty -> Empty
| Item (data, next) -> data)
let tl = (fun (list:'a my_list) ->
match list with
| Empty -> Empty
| Item (data, next) -> next)
let rec nth = (fun (list:'a my_list) (nb:int) ->
match nb with
| i when nb > length list -> invalid_arg "The list is too short"
| _ ->
match list with
| Empty -> invalid_arg "The list is Empty"
| Item (data, next) ->
if nb == 0
then data
else nth next (nb - 1))
let rec append = (fun (list1:'a my_list) (list2:'a my_list) ->
match list1 with
| Empty -> list2
| Item (data, next) -> Item (data, append next list2))
let rec rev = (fun (list:'a my_list) ->
match list with
| Empty -> Empty
| Item (data, next) -> append (rev next) (Item (data, Empty)))
let rec rev_append = (fun (list1:'a my_list) (list2:'a my_list) ->
let list = rev list1 in
match list with
| Empty -> list2
| Item (data, next) -> Item (data, append next list2))
let rec exists = (fun (f:'a -> bool) (list:'a my_list) ->
match list with
| Empty -> false
| Item (data, next) -> f data || exists f list)
let rec iter = (fun (f:'a -> 'b) (list:'a my_list) ->
match list with
| Empty -> ()
| Item (data, next) -> f data; iter f next)
let rec map = (fun (f:'a -> 'b) (list:'a my_list) ->
match list with
| Empty -> Empty
| Item (data, next) -> Item (f data, map f next))
let rec mem = (fun (elem:'a) (list:'a my_list) ->
match list with
| Empty -> false
| Item (data, next) ->
if compare data elem = 0
then true
else mem elem list)
let rec memq = (fun (elem:'a) (list:'a my_list) ->
match list with
| Empty -> false
| Item (data, next) ->
if data = elem
then true
else mem elem list)
let rec filter = (fun (f:'a -> bool) (list:'a my_list) ->
match list with
| Empty -> Empty
| Item (data, next) ->
if f data = true
then Item (data, filter f next)
else filter f next)
let rec assoc = (fun (key:'a) (list:('a * 'b) my_list) ->
match list with
| Empty -> Empty
| Item ((data1, data2), next) ->
if compare data1 key = 0
then data2
else assoc key next)
let rec mem_assoc = (fun (key:'a) (list:('a * 'b) my_list) ->
match list with
| Empty -> false
| Item ((data1, data2), next) ->
if compare data1 key = 0
then true
else mem_assoc key next)
let rec remove_assoc = (fun (key:'a) (list:('a * 'b) my_list) ->
match list with
| Empty -> list
| Item ((data1, data2), next) ->
if compare key data1 = 0
then Item ((data1, data2), next)
else remove_assoc key next)
let rec for_all = (fun (f:('a -> bool)) (list:'a my_list) ->
match list with
| Empty -> false
| Item (data, next) ->
if f data = false
then false
else for_all f list)
let rec fold_left = (fun f (elem:'a) (list:'b my_list) ->
match list with
| Empty -> Empty
| Item (data, next) -> fold_left f (f elem data) next)
let rec split = (fun (l:('a * 'b) my_list) ->
match l with
| Empty -> (Empty, Empty)
| Item ((data1, data2), next) ->
let (d1, d2) = split next in (d1, d2))