forked from standardml/cmlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict-list.sml
executable file
·168 lines (143 loc) · 5.01 KB
/
dict-list.sml
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
functor ListPreDict (structure Key : ORDERED)
:> PRE_DICT where type key = Key.t
=
struct
type key = Key.t
type 'a dict = (key * 'a) list
exception Absent
val empty = []
val isEmpty = null
fun singleton key x = [(key, x)]
fun insert l key x =
(case l of
[] => [(key, x)]
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
(key, x) :: l
| EQUAL =>
(key, x) :: rest
| GREATER =>
(key', y) :: insert rest key x))
fun insert' l key x =
(case l of
[] =>
([(key, x)], false)
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
((key, x) :: l, false)
| EQUAL =>
((key, x) :: rest, true)
| GREATER =>
let
val (d, present) = insert' rest key x
in
((key', y) :: d, present)
end))
fun remove l key =
(case l of
[] => []
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS => l
| EQUAL => rest
| GREATER =>
(key', y) :: remove rest key))
fun remove' l key =
(case l of
[] => ([], false)
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS => (l, false)
| EQUAL => (rest, true)
| GREATER =>
let
val (d, present) = remove' rest key
in
((key', y) :: d, present)
end))
fun operate' l key absentf presentf =
(case l of
[] =>
(case absentf () of
NONE =>
(NONE, NONE, [])
| z as SOME x =>
(NONE, z, [(key, x)]))
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
(case absentf () of
NONE =>
(NONE, NONE, l)
| z as SOME x =>
(NONE, z, (key, x) :: l))
| EQUAL =>
(case presentf y of
NONE =>
(SOME y, NONE, rest)
| z as SOME x =>
(SOME y, z, (key, x) :: rest))
| GREATER =>
let
val (ante, post, rest') = operate' rest key absentf presentf
in
(ante, post, (key', y) :: rest')
end))
fun operate dict key absentf presentf =
let
val (x, y, d) = operate' dict key (SOME o absentf) (SOME o presentf)
in
(x, valOf y, d)
end
fun insertMerge dict key x f =
#3 (operate' dict key (fn () => SOME x) (SOME o f))
fun find l key =
(case l of
[] =>
NONE
| (key', x) :: rest =>
(case Key.compare (key, key') of
LESS =>
NONE
| EQUAL =>
SOME x
| GREATER =>
find rest key))
fun lookup l key =
(case l of
[] =>
raise Absent
| (key', x) :: rest =>
(case Key.compare (key, key') of
LESS =>
raise Absent
| EQUAL =>
x
| GREATER =>
lookup rest key))
fun member l key =
(case l of
[] =>
false
| (key', _) :: rest =>
(case Key.compare (key, key') of
LESS =>
false
| EQUAL =>
true
| GREATER =>
member rest key))
fun toList l = l
fun domain l = List.map (fn (key, _) => key) l
fun map f l = List.map (fn (key, x) => (key, f x)) l
fun foldl f base l = List.foldl (fn ((key, x), y) => f (key, x, y)) base l
fun foldr f base l = List.foldr (fn ((key, x), y) => f (key, x, y)) base l
val app = List.app
end
functor ListDict (structure Key : ORDERED)
:>
DICT where type key = Key.t
=
DictFun (ListPreDict (structure Key = Key))