forked from tianyicui/pfds-ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch2.ml
119 lines (99 loc) · 2.34 KB
/
ch2.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
exception Empty
exception Subscript
module type STACK =
sig
type 'a stack
val empty : 'a stack
val is_empty : 'a stack -> bool
val cons : 'a -> 'a stack -> 'a stack
val head : 'a stack -> 'a (* raises Empty if stack is empty *)
val tail : 'a stack -> 'a stack (* raises Empty if stack is empty *)
val (++) : 'a stack -> 'a stack -> 'a stack
val update : 'a stack -> int -> 'a -> 'a stack
end
module ListStack : STACK =
struct
type 'a stack = 'a list
let empty = []
let is_empty s =
s = []
let cons x s =
x :: s
let head = function
| [] -> raise Empty
| h :: _ -> h
let tail = function
| [] -> raise Empty
| _ :: t -> t
let rec (++) xs ys =
match xs with
| [] -> ys
| xh :: xt -> xh :: (xt ++ ys)
let rec update s i x =
match s with
| [] -> raise Subscript
| h :: t ->
if i = 0 then x :: t else h :: update t (i-1) x
end
module CustomStack : STACK =
struct
type 'a stack =
| Nil
| Cons of 'a * 'a stack
let empty = Nil
let is_empty = function
| Nil -> true
| _ -> false
let cons x s =
Cons (x, s)
let head = function
| Nil -> raise Empty
| Cons (h, _) -> h
let tail = function
| Nil -> raise Empty
| Cons (_, t) -> t
let rec (++) xs ys =
match xs with
| Nil -> ys
| Cons (xh, xt) -> Cons (xh, xt ++ ys)
let rec update s i x =
match s with
| Nil -> raise Subscript
| Cons (h, t) ->
if i = 0 then Cons (x, t) else Cons (h, update t (i-1) x)
end
module type SET =
sig
type elem
type set
val empty : set
val insert : elem -> set -> set
val member : elem -> set -> bool
end
module type ORDERED =
sig
type t
val compare : t -> t -> int
end
module UnbalancedSet (Element : ORDERED) : (SET with type elem = Element.t) =
struct
type elem = Element.t
type set =
| Empty
| Tree of set * elem * set
let empty = Empty
let rec member x = function
| Empty -> false
| Tree (l, y, r) ->
match Element.compare x y with
| 0 -> true
| n when n < 0 -> member x l
| _ -> member x r
let rec insert x = function
| Empty -> Tree (Empty, x, Empty)
| Tree (l, y, r) as s ->
match Element.compare x y with
| 0 -> s
| n when n < 0 -> Tree (insert x l, y, r)
| _ -> Tree (l, y, insert x r)
end