-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.sml
65 lines (50 loc) · 1.42 KB
/
dict.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
(**
* @author YAMATODANI Kiyoshi
* @copyright 2010, Tohoku University.
*)
structure Dictionary =
struct
datatype dict = Dict of (string * DataTypes.DEC) list
exception NotFound
fun create () = Dict [];
fun exists (Dict ls) name =
List.exists (fn (n, v) => (n = name)) ls
fun lookup (Dict ((n, v) :: others)) name =
if n = name
then v
else lookup (Dict others) name
| lookup (Dict []) name = raise NotFound;
fun size (Dict ls) = List.length ls;
fun isEmpty (Dict []) = true
| isEmpty _ = false;
fun update (Dict ts) name value =
let
fun inup checked ((n, v) :: others) =
if n = name then
(n, value) :: (checked @ others)
else
inup ((n, v) :: checked) others
| inup checked [] = (name, value) :: checked
in
Dict (inup [] ts)
end;
fun remove (Dict ts) name =
let
fun rm checked ((n, v) :: others) =
if n = name
then rm checked others
else rm ((n, v) :: checked) others
| rm checked [] = checked
in
Dict (rm [] ts)
end
fun aslist (Dict ls) = ls;
fun keys (Dict ((n, _) :: others)) = n :: (keys (Dict others))
| keys (Dict []) = [];
fun items (Dict ((_, v) :: others)) = v::(items (Dict others))
| items (Dict []) = [];
fun mapkeys (Dict ls) f =
Dict (map (fn (k, v) => (f k, v)) ls);
fun mapitems (Dict ls) f =
Dict (map (fn (k, v) => (k, f v)) ls);
end;