forked from CSCI305/csci305-ml-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_lab.sml
58 lines (45 loc) · 1.85 KB
/
ml_lab.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
(***************************************************************
*
* CSCI 305 - ML Programming Lab
*
* <firstname> <lastname>
* <email-address>
*
***************************************************************)
(* Define your data type and functions here *)
(* Simple function to stringify the contents of a Set of characters *)
fun stringifyCharSet Empty = ""
| stringifyCharSet (Set(y, ys)) = Char.toString(y) ^ " " ^ stringifyCharSet(ys);
(* Simple function to stringify the contents of a Set of ints *)
fun stringifyIntSet Empty = ""
| stringifyIntSet (Set(w, ws)) = Int.toString(w) ^ " " ^ stringifyIntSet(ws);
(* Simple function to stringify the contents of a Set of strings *)
fun stringifyStringSet Empty = ""
| stringifyStringSet (Set(z, zs)) = z ^ " " ^ stringifyStringSet(zs);
(* Simple function that prints a set of integers *)
fun print_int x = print ("{ " ^ stringifyIntSet(x) ^ "}\n");
(* Simple function that prints a set of strings *)
fun print_str x = print ("{ " ^ stringifyStringSet(x) ^ "}\n");
(* Simple function that prints a set of characters *)
fun print_chr x = print ("{ " ^ stringifyCharSet(x) ^ "}\n");
list2Set [1, 3, 2];
list2Set [#"a", #"b", #"c"];
list2Set [];
list2Set [6, 2, 2];
list2Set ["x", "y", "z", "x"];
(* Question 1 *)
f [3, 1, 4, 1, 5, 9]
(* Question 5 *)
val quest5 = isMember "one" (list2Set ["1", "2", "3", "4"]);
print ("\nQuestion 5: " ^ Bool.toString(quest5) ^ "\n");
(* Question 7 *)
val quest7 = list2Set ["it", "was", "the", "best", "of", "times,", "it", "was", "the", "worst", "of", "times"];
print "\nQuestion 7: ";
print_str quest7;
print "\n";
(* Question 9 *)
print "\nQuestion 9: ";
print_str (union (list2Set ["green", "eggs", "and"]) (list2Set ["ham"]));
(* Question 10 *)
print "\nQuestion 10: ";
print_str (intersect (list2Set ["stewed", "tomatoes", "and", "macaroni"]) (list2Set ["macaroni", "and", "cheese"]));