addrbook.cd 2 KB
Newer Older
1
2
3
4
5
6
type Content = [(Name Addr Tel?)*];;
type Addrbook = <addrbook>Content;;
type Name = <name>[String];;
type Addr = <addr>[String];;
type Tel = <tel>[String];;

7

8
9
10
11
12
13
14
15
16
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ];;
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
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ];;
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ];;
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ];;
44

45
46

(*
47
48
49
50
51
52
53
54
55
56
57
58

(* converting an address book into a telephone list *)

fun mkTelList ([ (Name Addr Tel?)* ] -> [ (Name Tel)* ])
| [ <name>n <addr>a <tel>t ; rest] -> [ <name>n <tel>t ; mkTelList rest] 
| [ <name>n <addr>a; rest] -> mkTelList rest
| [ ] -> [ ]
;;

fun mkTelList (Addrbook -> [ (Name Tel)* ])
<_>[ ( ( (x::Name) Addr (x::Tel) ) | _ )* ] ->  x
;;
59
*)
60
61
62
63
64
65
66
67
68
69
70
71

fun (Int -> Addrbook) x ->
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ] 
;;
72
73
74
75
76
77
78
79
80
81
82
83
84
(*
fun (Int -> Addrbook) x ->
<addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ] 
;;
*)
85

86
87

(*
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
match <addrbook>[
  <name>["Haruo Hosoya"]
  <addr>["Tokyo"]
  <name>["Benjamin Pierce"]
  <addr>["Philadelphia"] 
  <tel>["123-456-789"]
  <name>["Peter Buneman"]
  <addr>["Scotland"]
 ] with
<_>[ ( ( (x::Name) Addr (x::Tel) ) | _ )* ] ->  x;;

(*
(* the pattern extract the full sequence of subelements *)
match ex with addrbook:[;a] -> mkTelList a;;
*)
103
104

*)