-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlists.sml
370 lines (286 loc) · 6.92 KB
/
lists.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
(*
Author@Krishna
Problems from https://sites.google.com/site/prologsite/prolog-problems/1
Instead of built in functions, I'll be using recursion, let expressions, and pattern matching as much as possible.
*)
structure PrologLists :> PROBLEMS99 =
struct
exception EmptyList
exception BadNum of int
(*1.01*)
fun last_item (lst) =
case lst of
[]=> raise EmptyList
| xs::[] => SOME xs
| xs::ys => last_item(ys)
(*1.02*)
fun penultimate (lst) =
case lst of
[]=> raise EmptyList
| xs::[] => NONE
| xs::ys::[] => SOME xs
| xs::ys::zs => penultimate(ys::zs)
(*1.03*)
fun nth_item (lst, n) =
case (lst, n) of
([],num) => raise Empty
| (xs::ys, 1) => SOME xs
| (xs::ys, num) => nth_item(ys, num-1)
(*1.04*)
fun size (lst) =
case lst of
[]=> 0
| xs::ys => 1 + size (ys)
(*1.05*)
fun reverse (lst) =
case lst of
[]=> []
| xs::ys => reverse(ys) @ [xs]
(*helper function to avoid polyEqual warnings*)
fun equals (a,b) =
a=b
(*1.06*)
fun is_palindrome (lst) =
equals(lst, reverse(lst))
(*1.07*)
fun flatten (lst) =
let fun unpack (lsts) =
case lsts of
[]=> []
| x::y => x :: unpack y
in
case lst of
[]=> []
| xs::ys => unpack xs @ flatten ys
end
(*1.08*)
fun compress (lst) =
case lst of
[]=> []
| xs::ys::zs => if equals (xs, ys) then compress (ys::zs)
else xs::compress(ys::zs)
| xs::ys => xs::compress(ys)
fun compress2 (lst) =
let fun compare (from, to) =
case from of
[]=> []
| xs::ys => if equals(xs, to) then compare(ys, to)
else xs :: compress2(ys)
in
if null lst then [] else
compare(lst, hd lst)
end
(*1.09*)
fun pack (lst) =
let fun compare (from,to,acc) =
case from of
[]=> acc::[]
| xs::ys => if equals(xs, to) then compare (ys, xs, to::acc)
else acc::compare (ys,xs,[xs])
in
if null lst then []
else compare (lst, hd lst,[])
end
(*1.10*)
fun encode (lst) =
let fun compare (from, to, count) =
case from of
[]=> [(to,count)]
| xs::ys => if equals (xs, to) then compare (ys, to, count+1)
else [(to, count)] @ encode(xs::ys)
in
if null lst then []
else compare (lst, hd lst, 0)
end
(*1.11*)
fun encode_modified (lst) =
let fun traverse (packed_list) =
case packed_list of
[]=> []
| xs::ys => (hd xs, size xs):: traverse ys
in
traverse (pack lst)
end
(*helper function*)
fun repeat (item, count) =
if count = 0 then []
else item::repeat(item, count-1)
(*1.12*)
fun decode (lst) =
case lst of
[]=> []
| xs::ys => repeat xs @decode(ys)
(*1.14*)
fun duplicate (lst) =
case lst of
[]=> []
| xs::ys => xs::[xs]@duplicate(ys)
(*1.15*)
fun duplicateN (N,lst) =
case (N, lst) of
(_,[])=>[]
| (num, xs::ys) => repeat(xs, num)@duplicateN(num, ys)
(*1.16*)
fun drop (N, lst) =
let fun helper ( M, sth) =
case sth of
[]=> []
| xs::ys => if M = 0 then xs :: drop (N, ys)
else helper (M-1,ys)
in
helper(N, lst)
end
(*1.17*)
(*TODO::edit it for N = 0*)
fun split (lst, N) =
let fun dissect (sth, acc, M) =
case sth of
[]=>[]
| xs::ys => if M > 0 then dissect (ys, acc@[xs], M-1)
else [acc] @ [sth]
in
if null lst then []
else dissect (lst, [], N)
end
(*1.18*)
fun slice (n1,n2,lst) =
if null lst then []
else if n1 > 1 then slice(n1-1,n2-1,tl lst)
else let fun chop (n2, sth) =
if n2 < 1 then []
else hd sth :: chop (n2-1, tl sth)
in
chop(n2,lst)
end
(*1.19*)
fun rotate (lst, N) =
let fun shift_items (M, sth, acc) =
case sth of
[]=>[]
| xs::ys => if M > 0 then shift_items (M-1, ys, acc@[xs])
else sth@acc
in
if N = 0 then lst
else shift_items (N, lst, [])
end
(*1.20*)
fun remove_at (lst, N) =
(*
if N < 1 then raise BadNum (N)
*)
case lst of
[]=> []
| xs::ys => if N > 1 then xs:: remove_at(ys, N-1)
else ys
(*1.21*)
fun insert_at (alpha, lst, N) =
case lst of
[]=> if N = 1 then [alpha] else []
| xs::ys => if N > 1 then xs :: insert_at(alpha, ys, N-1)
else alpha :: lst
(*1.22*)
fun range (num1, num2) =
if num1<num2 then num1::range(num1+1,num2)
else [num2]
(*1.23*)
fun rnd_select (lst, num) =
let
val x = Random.rand(num, size lst)
val y = Random.randRange (1, size lst) x
val item = valOf(nth_item(lst, y))
in
if num > 0 then
item :: rnd_select(List.filter(fn x=> x <> item) lst, num-1)
else []
end
(*1.24*)
fun rnd_select_range (low, high) =
let
val lst = range(low, high)
in
rnd_select(lst, high-low)
end
(*1.26*)
fun combination (num, lst) =
case (num,lst) of
(0,_) =>[[]]
| (_,[]) => []
| (m,x::xs) => map (fn y => x::y) (combination (m-1,xs)) @ combination(m,xs)
(*1.27
Multinomial coefficients
*)
(*helper function for sorting*)
fun quicksort (lst) =
case lst of
[] => []
| hd::[] => [hd]
| pivot::rest => let
val (left, right) = List.partition (fn x => size(x) < size(pivot)) rest
in
quicksort left @ [pivot] @ quicksort right
end
(*1.28a*)
fun lsort (lnlist) =
quicksort(lnlist)
(*helper functions for 1.28b*)
fun count (item,lst,acc) =
case lst of
[]=> acc
| xs::ys => if item = xs then count(item, ys, acc+1)
else count(item, ys, acc)
fun delete (item, lst) =
if null lst then []
else if item = hd lst
then delete(item, tl lst)
else hd(lst)::delete(item, tl lst)
fun strip_duplicates (lst) =
case lst of
[]=>[]
| xs::ys => xs::strip_duplicates(delete(xs,ys))
fun size_it (lst) =
case lst of
[]=> []
| xs::ys => List.length(xs)::size_it(ys)
fun quicksort_tuple (lst):(int*int) list =
case lst of
[] => []
| hd::[] => [hd]
| pivot::rest => let
val (left, right) = List.partition (fn x => #2 x < #2 pivot) rest
in
quicksort_tuple left @ [pivot] @ quicksort_tuple right
end
fun loop_given_list (num,lst1) =
case lst1 of
[]=>[]
| xs::ys => if num = List.length(xs) then xs::loop_given_list(num,ys)
else loop_given_list(num,ys)
fun frequency_lists (lst1,lst2) =
case lst1 of
[]=> []
| xs::ys => (xs,count(xs,lst2,0))::frequency_lists(ys,lst2)
(*1.28b*)
(* Most likely a overkill.
a)->get the sizes of the list
b)->get unique sizes
c)->get frequency by comparing a, and b.
d)->quicksort by frequency
e)->compare #2 of each tuple and sort return lists accordingly
*)
fun lfsort (lnlist)=
let
val list_sizes = size_it (lnlist)
val list_unique_sizes = strip_duplicates(list_sizes)
val freq_tuples = frequency_lists (list_unique_sizes, list_sizes)
val quic = quicksort_tuple(freq_tuples)
val sorted_size_list = List.map(fn x=> #1 x) quic
in
let fun loop_through_ssl (lst) =
case lst of
[]=> []
| xs::ys => loop_given_list (xs, lnlist)@loop_through_ssl(ys)
in
loop_through_ssl(sorted_size_list)
end
end
end