-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.sml
287 lines (188 loc) · 5.04 KB
/
3.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
(* Coursera Programming Languages, Homework 3, Provided Code *)
exception NoAnswer
datatype pattern = Wildcard
| Variable of string
| UnitP
| ConstP of int
| TupleP of pattern list
| ConstructorP of string * pattern
datatype valu = Const of int
| Unit
| Tuple of valu list
| Constructor of string * valu
fun g f1 f2 p =
let
val r = g f1 f2
in
case p of
Wildcard => f1 ()
| Variable x => f2 x
| TupleP ps => List.foldl (fn (p,i) => (r p) + i) 0 ps
| ConstructorP(_,p) => r p
| _ => 0
end
(**** for the challenge problem only ****)
datatype typ = Anything
| UnitT
| IntT
| TupleT of typ list
| Datatype of string
(**** you can put all your code here ****)
infix ~>
fun x ~> f = f(x)
(* 1 *)
fun first_char str = String.sub(str, 0)
fun only_capitals(strs) = strs ~> List.filter (fn str => str ~> first_char ~> Char.isUpper)
(* 2 *)
fun longest_string1 (strs) =
List.foldl (
fn (str1, str2) =>
if (size(str2) >= size(str1)) then
str2
else
str1
)
"" strs
(* 3 *)
fun longest_string2 (strs) =
List.foldl (
fn (str1, str2) =>
if (size(str2) > size(str1)) then
str2
else
str1
)
"" strs
(* 4 *)
fun longest_string_helper comparator =
let
fun intern_helper ([]) = ""
| intern_helper (str::[]) = str
| intern_helper (str1::str2::strs) =
if (comparator(size str1, size str2)) then
intern_helper(str2::strs)
else
intern_helper(str1::strs)
in
intern_helper
end
val longest_string3 = longest_string_helper (fn (len1, len2) => len2 > len1)
val longest_string4 = longest_string_helper (fn (len1, len2) => len2 >= len1)
(* 5 *)
val longest_capitalized = longest_string2 o only_capitals
(* 6 *)
val rev_string = implode o List.rev o explode
(* 7 *)
fun first_answer somefun =
let
fun second_fun [] = raise NoAnswer
| second_fun (el::alist) =
if (isSome(somefun(el))) then
valOf(somefun(el))
else
second_fun alist
in
second_fun
end
(* 8 *)
fun all_answers f1 [] = SOME[]
| all_answers f1 alist =
let
fun iterate ([], acc) = acc
| iterate (el::lst, acc) =
if (isSome(f1 el)) then
iterate(lst, acc @ valOf(f1 el) )
else
[]
val ret = iterate (alist, [])
in
case ret of
[] => NONE
| _ => SOME ret
end
(* 9 *)
(* a *)
fun count_wildcards patrn =
let
fun wildcard_matcher _ = 1
fun second_matcher _ = 0
in
g wildcard_matcher second_matcher patrn
end
(* b *)
fun count_wild_and_variable_lengths patrn =
let
fun wildcard_matcher _ = 1
fun var_matcher str = String.size(str)
in
g wildcard_matcher var_matcher patrn
end
(* c *)
fun count_some_var (str, patrn) =
let
fun wildcard_matcher _ = 0
fun var_matcher str1 =
if (str = str1) then
1
else
0
in
g wildcard_matcher var_matcher patrn
end
(* 10 *)
(* get all the variable strings from a list of patterns *)
fun strings_from_lst (Variable(str)::tail) = str:: strings_from_lst(tail)
| strings_from_lst (TupleP(lst)::tail) = strings_from_lst(lst) @ strings_from_lst(tail)
| strings_from_lst (_::tail) = strings_from_lst(tail)
| strings_from_lst (_) = []
(* get all var strings from a pattern using the helper above *)
fun strings_from_pat (Variable(str)) = [str]
| strings_from_pat (TupleP(lst)) = strings_from_lst(lst)
| strings_from_pat (_) = []
(* check if the list contains only unique entries. Not optimal. In worst case it is almost
quadratic, might be better to sort the list beforehand. Basically, we could find
identical entries at the sorting stage. Would be n*log(n) *)
fun unique_in_list([]) = true
| unique_in_list(str::lst) =
if (List.exists (fn (str_from_list) => str = str_from_list) lst) then
false
else
unique_in_list(lst)
(* now lets pipe pattern to get the strings from it and then check if they are unique *)
fun check_pat(ptrn) = ptrn ~> strings_from_pat ~> unique_in_list
(* this is 14 lines of code :*)
(* 11 *)
exception DoesntMatch
fun match_list (_, Wildcard) = []
| match_list (a_value, Variable str) = [(str, a_value)]
| match_list (Unit, UnitP) = []
| match_list (Const x, ConstP y) =
if (x = y) then
[]
else
raise DoesntMatch
| match_list (Tuple([]), TupleP ps) = raise DoesntMatch
| match_list (Tuple vs, TupleP([])) = raise DoesntMatch
| match_list (Tuple (v::vs), TupleP (p::ps)) =
match_list (v, p) @ match_list (Tuple(vs), TupleP(ps))
| match_list (Constructor (str1, v), ConstructorP(str2, p)) =
if (str1 <> str2) then
raise DoesntMatch
else
match_list(v, p)
| match_list (_, _) = raise DoesntMatch
fun match (a_value, patrn) = (
SOME (match_list(a_value, patrn))
)
handle DoesntMatch => NONE
(* 12 *)
fun first_match a_value ([]) = NONE
| first_match a_value (patrn::tail) =
let
val match_result = match(a_value, patrn)
in
if isSome(match_result) then
match_result
else
first_match a_value tail
end