-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw2provided.txt
174 lines (140 loc) · 4.39 KB
/
hw2provided.txt
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
169
170
171
172
173
174
(* Dan Grossman, Coursera PL, HW2 Provided Code *)
(* if you use this function to compare two strings (returns true if the same
string), then you avoid several of the functions in problem 1 having
polymorphic types that may be confusing *)
fun same_string(s1 : string, s2 : string) =
s1 = s2
(* put your solutions for problem 1 here *)
fun all_except_option(search_string, strings ) =
let fun f (xs,acc) =
case xs of
[] => acc
| x :: xs' => if same_string(search_string,x)
then f(xs', acc)
else f(xs', x :: acc)
in
let
val sub_strings = f(strings,[])
in
if length(sub_strings) = length(strings)
then NONE
else SOME sub_strings
end
end
fun get_substitutions1( strings_list_list, search_string) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = all_except_option(search_string,xs);
in
if sub_strings_list = NONE
then f( ys, acc)
else f( ys, getOpt(sub_strings_list,[]) @ acc)
end
in
f(strings_list_list,[])
end
fun get_substitutions2( strings_list_list, search_string) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = all_except_option(search_string,xs);
in
if sub_strings_list = NONE
then f( ys, acc)
else f( ys, getOpt(sub_strings_list,[]) @ acc)
end
in
f(strings_list_list,[])
end
fun similar_names( strings_list_list, {first,middle,last}) =
let
fun make_name(xs,acc) =
case xs of
[] => acc
| x :: xs' => make_name(xs',{first=x, middle=middle, last=last} :: acc);
fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => let
val sub_strings_list = get_substitutions2(xs,first);
in
if SOME sub_strings_list = NONE
then f( ys, acc)
else f( ys, acc @ make_name(sub_strings_list,[]))
end
in
f([strings_list_list],[{first=first,middle=middle,last=last}])
end
(* you may assume that Num is always used with values 2, 3, ..., 10
though it will not really come up *)
datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int
type card = suit * rank
datatype color = Red | Black
datatype move = Discard of card | Draw
exception IllegalMove
(* put your solutions for problem 2 here *)
exception NoCardsFound
fun card_color(card) =
case card of
(Spades , _) => Black
|(Clubs , _) => Black
|(Diamonds , _) => Red
|(Hearts , _) => Red
fun card_value(card) =
case card of
(_ , Jack) => 10
|(_ , Queen) => 10
|(_ , King) => 10
|(_ , Ace) => 11
|(_ , Num x) => x
fun same(c1 , c2 ) =
c1 = c2
fun all_cards_except_option(pick, cards ) =
let fun f (xs,acc) =
case xs of
[] => acc
| x :: xs' => if same(pick,x)
then f(xs', acc)
else f(xs', x :: acc)
in
let
val cards_left = f(cards,[])
in
if length(cards_left) = length(cards)
then NONE
else SOME cards_left
end
end
fun remove_card( deck , pick , ex) =
let
val cards_left = all_cards_except_option(pick,deck)
in
if cards_left = NONE
then raise ex
else getOpt(cards_left,[])
end
fun all_same_color( cards) =
case cards of
[] => true
| x::[] => true
| first :: (second :: rest) => (card_color(first) = card_color(second) andalso all_same_color(second :: rest))
fun sum_cards( cards) =
let fun f(xs,acc) =
case xs of
[] => acc
| xs :: ys => f(ys, card_value(xs) + acc)
in
f(cards,0)
end
fun score(cards,goal) =
let
val sum = sum_cards(cards);
in
if sum <= goal
then goal - sum
else sum - goal
end