|
| 1 | +use "la1.sml"; |
| 2 | + |
| 3 | +val score = 0.0; |
| 4 | +fun check (a,b,score) = if a = b then score else 0.0; |
| 5 | +fun checkR (a,b,score) = if abs(a - b) <= 0.00001 then score else 0.0; |
| 6 | +fun eqR (a,b) = abs(a-b) <= 0.00001; |
| 7 | +fun abs r = if r >= 0.0 then r else ~r; |
| 8 | +fun eqRList ([],[]) = true |
| 9 | +| eqRList ([],x::xs) = false |
| 10 | +| eqRList (x::xs,[]) = false |
| 11 | +| eqRList (x::xs,y::ys) = eqR(x,y) andalso eqRList(xs,ys); |
| 12 | +fun checkRList (l1,l2,score) = if eqRList(l1,l2) then score else 0.0; |
| 13 | + |
| 14 | +val iList = [2,6,1,7,8]; |
| 15 | +val cList = explode "wildcats"; |
| 16 | +val emptyList : int list = []; |
| 17 | +val sList = ["Phoenix", "Little Rock", "Montgomery", "Hartford", "Denver"]; |
| 18 | +val lList = [[],[1,2,3],[4,5,6,7]]; |
| 19 | +val pList = [(1,2),(4,5),(~2,3),(8,4)]; |
| 20 | +val rList = [1.5, 2.5, 3.5]; |
| 21 | +val clList = map explode sList; |
| 22 | + |
| 23 | +print "\ntriangle\n"; (* 1 point *) |
| 24 | +val act = triangle(2,3,4); |
| 25 | +val score = score + check(true,act,0.5); |
| 26 | +val act = triangle(3,1,1); |
| 27 | +val score = score + check(false,act,0.5); |
| 28 | + |
| 29 | +print "\ntriangleR\n"; (* 1 point *) |
| 30 | +val act = triangleR(3.2,4.1,2.5); |
| 31 | +val score = score + check(true,act,0.5); |
| 32 | +val act = triangleR(1.2,3.5,1.3); |
| 33 | +val score = score + check(false,act,0.5); |
| 34 | + |
| 35 | +print "\ncycle\n"; (* 2 points *) |
| 36 | +val act = cycle(3,iList); |
| 37 | +val score = score + check([7,8,2,6,1],act,0.5); |
| 38 | +val act = implode(cycle(10,cList)); |
| 39 | +val score = score + check("ldcatswi",act,0.5); |
| 40 | +val act = cycle(0,iList); |
| 41 | +val score = score + check([2,6,1,7,8],act,0.5); |
| 42 | +val act = cycle(5,emptyList); |
| 43 | +val score = score + check([],act,0.5); |
| 44 | + |
| 45 | +print "\nmirror\n"; (* 2 points *) |
| 46 | +val x = mirror emptyList; |
| 47 | +val score = score + check([],x,0.5); |
| 48 | +val x = mirror iList; |
| 49 | +val score = score + check([2,6,1,7,8,8,7,1,6,2],x,0.5); |
| 50 | +val x = implode(mirror cList); |
| 51 | +val score = score + check("wildcatsstacdliw",x,0.5); |
| 52 | +val x = mirror [5]; |
| 53 | +val score = score + check(x, [5,5], 0.5); |
| 54 | + |
| 55 | +print "\ngtList\n"; (* 2 points *) |
| 56 | +val x = gtList([],4); |
| 57 | +val score = score + check(x,[], 0.5); |
| 58 | +val x = gtList(iList, 3); |
| 59 | +val score = score + check(x,[6,7,8],0.5); |
| 60 | +val x = gtList(iList, 9); |
| 61 | +val score = score + check(x,[],0.5); |
| 62 | +val x = gtList(iList,0); |
| 63 | +val score = score + check(x,iList,0.5); |
| 64 | + |
| 65 | +print "\nsuffix\n"; (* 2 points *) |
| 66 | +val x = suffix([1,7,8],iList); |
| 67 | +val score = score + check(x,true,0.5); |
| 68 | +val x = suffix([1,7],iList); |
| 69 | +val score = score + check(x,false,0.5); |
| 70 | +val x = suffix([1,7,8],[1,7,8]); |
| 71 | +val score = score + check(x,true,0.5); |
| 72 | +val x = suffix(explode("cats"),cList); |
| 73 | +val score = score + check(x,true,0.5); |
| 74 | + |
| 75 | +print "\nget\n"; (* 2 points *) |
| 76 | +val x = get (iList, 0); |
| 77 | +val score = score + check(x, 2, 0.5); |
| 78 | +val x = get (sList, 3); |
| 79 | +val score = score + check(x, "Hartford", 0.5); |
| 80 | +val x = get (cList, 5); |
| 81 | +val score = score + check(x, #"a", 0.5); |
| 82 | +val x = get (lList, 2); |
| 83 | +val score = score + check(x, [4,5,6,7], 0.5); |
| 84 | + |
| 85 | +print "\nsubList\n"; (* 4 points *) |
| 86 | +val x = subList(cList, 6, 6); |
| 87 | +val score = score + check(x, [#"t"], 1.0); |
| 88 | +val x = implode(subList((explode "university of arizona"), 3, 8)); |
| 89 | +val score = score + check(x, "versit", 1.0); |
| 90 | +val x = subList (iList, 1, 2); |
| 91 | +val score = score + check(x, [6,1], 1.0); |
| 92 | +val x = subList (sList, 0, 4); |
| 93 | +val score = score + check(x, sList, 1.0); |
| 94 | + |
| 95 | +print "\nreverse\n"; (* 3 points *) |
| 96 | +val x = reverse emptyList; |
| 97 | +val score = score + check(x, emptyList, 1.0); |
| 98 | +val x = reverse iList; |
| 99 | +val score = score + check(x, [8,7,1,6,2], 1.0); |
| 100 | +val x = implode(reverse cList); |
| 101 | +val score = score + check(x, "stacdliw", 1.0); |
| 102 | + |
| 103 | +print "\napply\n"; (* 3 points *) |
| 104 | +val x = apply(pList, (op -)); |
| 105 | +val score = score + check(x, [~1,~1,~5,4], 1.0); |
| 106 | +val x = implode(apply(sList, (fn s => hd(explode s)))); |
| 107 | +val score = score + check(x, "PLMHD", 1.0); |
| 108 | +val x = apply(rList, round); |
| 109 | +val score = score + check(x, [2,2,4], 1.0); |
| 110 | + |
| 111 | +print "\ncollapse\n"; (* 3 points *) |
| 112 | +val x = collapse(iList,0,(op +)); |
| 113 | +val score = score + check(x,24,1.0); |
| 114 | +val x = collapse(pList,1,(fn ((a,b),c) => (a+b)*c)); |
| 115 | +val score = score + check(x,324,1.0); |
| 116 | +val x = collapse(rList,2.0,(op * )); |
| 117 | +val score = score + checkR(x,26.25,1.0); |
| 118 | + |
| 119 | +print "\nquicksort\n"; (* 4 points *) |
| 120 | +val x = quicksort (op <=) iList; |
| 121 | +val score = score + check(x, [1,2,6,7,8], 1.0); |
| 122 | +val x = quicksort (op >=) [1,4,2,5,3,1,2,3]; |
| 123 | +val score = score + check(x, [5,4,3,3,2,2,1,1], 1.0); |
| 124 | +val x = implode(quicksort (op <=) cList); |
| 125 | +val score = score + check(x, "acdilstw", 1.0); |
| 126 | +val x = quicksort (op >=) rList; |
| 127 | +val score = score + checkRList(x,[3.5,2.5,1.5],1.0); |
| 128 | + |
| 129 | +print "\nbubbleSort\n"; (* 4 points *) |
| 130 | +val x = bubbleSort (op <=) iList; |
| 131 | +val score = score + check(x, [1,2,6,7,8], 1.0); |
| 132 | +val x = bubbleSort (op >=) [1,4,2,5,3,1,2,3]; |
| 133 | +val score = score + check(x, [5,4,3,3,2,2,1,1], 1.0); |
| 134 | +val x = implode(bubbleSort (op <=) cList); |
| 135 | +val score = score + check(x, "acdilstw", 1.0); |
| 136 | +val x = bubbleSort (op >=) rList; |
| 137 | +val score = score + checkRList(x,[3.5,2.5,1.5],1.0); |
| 138 | + |
| 139 | +print "\ninsertionSort\n"; (* 4 points *) |
| 140 | +val x = insertionSort (op <=) iList; |
| 141 | +val score = score + check(x, [1,2,6,7,8], 1.0); |
| 142 | +val x = insertionSort (op >=) [1,4,2,5,3,1,2,3]; |
| 143 | +val score = score + check(x, [5,4,3,3,2,2,1,1], 1.0); |
| 144 | +val x = implode(insertionSort (op <=) cList); |
| 145 | +val score = score + check(x, "acdilstw", 1.0); |
| 146 | +val x = insertionSort (op >=) rList; |
| 147 | +val score = score + checkRList(x,[3.5,2.5,1.5],1.0); |
| 148 | + |
| 149 | +print "\nsubstring\n"; (* 3 points *) |
| 150 | +val x = substring "zon" "arizona"; |
| 151 | +val score = score + check(x, true, 1.0); |
| 152 | +val x = substring "niva" "University"; |
| 153 | +val score = score + check(x, false, 1.0); |
| 154 | +val x = substring "frog" "frog"; |
| 155 | +val score = score + check(x, true, 1.0); |
| 156 | + |
| 157 | +print "\nindexOf\n"; (* 2 points *) |
| 158 | +val x = indexOf 5 []; |
| 159 | +val score = score + check(x, ~1, 0.5); |
| 160 | +val x = indexOf 7 iList; |
| 161 | +val score = score + check(x, 3, 0.5); |
| 162 | +val x = indexOf 5 iList; |
| 163 | +val score = score + check(x, ~1, 0.5); |
| 164 | +val x = indexOf "Denver" sList; |
| 165 | +val score = score + check(x, 4, 0.5); |
| 166 | + |
| 167 | +print "\ndec2BaseN\n"; (* 3 points *); |
| 168 | +val act = dec2BaseN 2 82; |
| 169 | +val score = score + check("1010010", act, 1.0); |
| 170 | +val act = dec2BaseN 8 100; |
| 171 | +val score = score + check("144", act, 1.0); |
| 172 | +val act = dec2BaseN 3 44; |
| 173 | +val score = score + check("1122", act, 1.0); |
| 174 | + |
| 175 | +print "\ndropNth\n"; (* 3 points *); |
| 176 | +val x = dropNth 2 [1,2,3,4,5]; |
| 177 | +val score = score + check(x, [1,3,5], 1.0); |
| 178 | +val x = dropNth 4 emptyList; |
| 179 | +val score = score + check(x, emptyList, 1.0); |
| 180 | +val x = implode(dropNth 3 cList); |
| 181 | +val score = score + check(x, "widcts", 1.0); |
| 182 | + |
| 183 | +print "\nflatten\n"; (* 2 points *) |
| 184 | +val x = flatten lList; |
| 185 | +val score = score + check(x, [1,2,3,4,5,6,7], 1.0); |
| 186 | +val x = implode(flatten clList); |
| 187 | +val score = score + check(x, "PhoenixLittle RockMontgomeryHartfordDenver", 1.0); |
| 188 | + |
| 189 | +print "\ncondenseLists\n"; (* 2 points *) |
| 190 | +val x = condenseLists (op +) 0 lList; |
| 191 | +val score = score + check(x, [0,6,22], 1.0); |
| 192 | +val x = condenseLists (fn (c,s) => ord c + s) 0 clList; |
| 193 | +val score = score + check(x, [731,1053,1073,826,612], 1.0); |
| 194 | + |
| 195 | +print "\nremove\n"; (* 2 points *) |
| 196 | +val x = remove (fn n => n mod 3 = 0) iList; |
| 197 | +val score = score + check(x, [2,1,7,8], 1.0); |
| 198 | +val x = implode(remove (fn c => c = #"w" orelse c = #"s") cList); |
| 199 | +val score = score + check(x, "ildcat", 1.0); |
| 200 | + |
| 201 | +print "\ntriplist\n"; (* 2 points *) |
| 202 | +val x = triplist iList; |
| 203 | +val score = score + check(x, [2,2,2,6,6,6,1,1,1,7,7,7,8,8,8], 1.0); |
| 204 | +val x = implode(triplist cList); |
| 205 | +val score = score + check(x, "wwwiiillldddcccaaatttsss", 1.0); |
| 206 | + |
| 207 | +print "\nrepeat\n"; (* 3 points *) |
| 208 | +val x = repeat iList 2; |
| 209 | +val score = score + check(x, [2,6,1,7,8,2,6,1,7,8], 1.0); |
| 210 | +val x = implode(repeat cList 5); |
| 211 | +val score = score + check(x, "wildcatswildcatswildcatswildcatswildcats", 1.0); |
| 212 | +val x = repeat emptyList 5; |
| 213 | +val score = score + check(x, emptyList, 1.0); |
| 214 | + |
| 215 | +print "\nfilterApply\n"; (* 2 points *) |
| 216 | +val x = filterApply iList (fn n => n mod 3 = 2) (fn x => x + 1); |
| 217 | +val score = score + check(x, [3,6,1,7,9], 1.0); |
| 218 | +val x = implode(filterApply cList (fn c => c = #"a") (fn c => chr (ord c + 1))); |
| 219 | +val score = score + check(x, "wildcbts", 1.0); |
| 220 | + |
| 221 | +print "\narithSeq\n"; (* 3 points *) |
| 222 | +val x = arithSeq 0 5 3; |
| 223 | +val score = score + check(x, [0,5,10], 1.0); |
| 224 | +val x = arithSeq 10 ~3 5; |
| 225 | +val score = score + check(x, [10, 7, 4, 1, ~2], 1.0); |
| 226 | +val x = arithSeq 5 0 6; |
| 227 | +val score = score + check(x, [5,5,5,5,5,5], 1.0); |
| 228 | + |
| 229 | +print ("\nelement\n"); (* 2 points *) |
| 230 | +val x = element 3 iList; |
| 231 | +val score = score + check(x, false, 0.5); |
| 232 | +val x = element 7 iList; |
| 233 | +val score = score + check(x, true, 0.5); |
| 234 | +val x = element 6 emptyList; |
| 235 | +val score = score + check(x, false, 0.5); |
| 236 | +val x = element #"i" cList; |
| 237 | +val score = score + check(x, true, 0.5); |
| 238 | + |
| 239 | +print("\nisSet\n"); (* 2 points *) |
| 240 | +val score = score + check(isSet iList, true, 0.5); |
| 241 | +val score = score + check(isSet emptyList, true, 0.5); |
| 242 | +val score = score + check(isSet (explode "banana"), false, 0.5); |
| 243 | +val score = score + check(isSet sList, true, 0.5); |
| 244 | + |
| 245 | +fun qs _ [] = [] |
| 246 | +| qs _ [x] = [x] |
| 247 | +| qs f l = |
| 248 | +let |
| 249 | + fun pivot p = foldr (fn (y,(lt, gte)) => if f(y,p) then (y::lt, gte) else (lt, y::gte)) ([], []); |
| 250 | + val (l1, l2) = pivot (hd l) (tl l); |
| 251 | +in |
| 252 | + ((qs f l1) @ [hd l] @ (qs f l2)) |
| 253 | +end; |
| 254 | + |
| 255 | +fun equal([],[]) = true |
| 256 | +| equal([],x::xs) = false |
| 257 | +| equal(x::xs,[]) = false |
| 258 | +| equal(x::xs,y::ys) = x=y andalso equal(xs,ys); |
| 259 | + |
| 260 | +fun same (l1, l2) = |
| 261 | +let |
| 262 | + val l1_sorted = qs (op <=) l1 |
| 263 | + val l2_sorted = qs (op <=) l2 |
| 264 | +in |
| 265 | + equal(l1_sorted, l2_sorted) |
| 266 | +end; |
| 267 | + |
| 268 | +fun sameC (l1 : char list, l2) = |
| 269 | +let |
| 270 | + val l1_sorted = qs (op <=) l1 |
| 271 | + val l2_sorted = qs (op <=) l2 |
| 272 | +in |
| 273 | + equal(l1_sorted, l2_sorted) |
| 274 | +end; |
| 275 | + |
| 276 | +val iSet1 = [1,2,3,4,5]; |
| 277 | +val iSet2 = [1,3,5,7,9,11,13]; |
| 278 | +val cSet1 = explode "arizon"; |
| 279 | +val cSet2 = explode "universty"; |
| 280 | + |
| 281 | +fun checkSets (set1, set2, score) = if same(set1, set2) then score else 0.0; |
| 282 | +fun checkSetsC (set1, set2, score) = if sameC(set2, set2) then score else 0.0; |
| 283 | + |
| 284 | +print("\nunion\n"); (* 2 points *) |
| 285 | +val score = score + checkSets(union(iSet1,iSet2),[1,2,3,4,5,7,9,11,13],0.5); |
| 286 | +val score = score + checkSets(union(iSet1,[]), iSet1, 0.5); |
| 287 | +val score = score + checkSetsC(union(cSet1,cSet2),(explode "arizonuvesty"),0.5); |
| 288 | +val score = score + checkSetsC(union([],cSet1),cSet1,0.5); |
| 289 | + |
| 290 | +print ("\nintersection\n"); (* 2 points *) |
| 291 | +val score = score + checkSets(intersection(iSet1,iSet2), [1,3,5], 0.5); |
| 292 | +val score = score + checkSets(intersection(iSet1,[]), [], 0.5); |
| 293 | +val score = score + checkSetsC(intersection(cSet1,cSet2), explode "rin", 0.5); |
| 294 | +val score = score + checkSetsC(intersection([],cSet1), [], 0.5); |
| 295 | + |
| 296 | +print("\ndifference\n"); (* 2 points *) |
| 297 | +val score = score + checkSets(difference(iSet1,iSet2),[2,4],0.5); |
| 298 | +val score = score + checkSets(difference(iSet1,[]), iSet1, 0.5); |
| 299 | +val score = score + checkSetsC(difference(cSet1,cSet2),(explode "azo"),0.5); |
| 300 | +val score = score + checkSetsC(difference([],cSet1),[],0.5); |
| 301 | + |
| 302 | +print ("\nxor\n"); (* 2 points *) |
| 303 | +xor(iSet1,iSet2); |
| 304 | +val score = score + checkSets(xor(iSet1,iSet2), [2,4,7,9,11,13], 0.5); |
| 305 | +val score = score + checkSets(xor(iSet1,[]), iSet1, 0.5); |
| 306 | +val score = score + checkSetsC(xor(cSet1,cSet2), explode "azouvesty", 0.5); |
| 307 | +val score = score + checkSetsC(xor([],cSet1), cSet1, 0.5); |
| 308 | + |
| 309 | +fun mem (_,[]) = false |
| 310 | +| mem (e,x::xs) = e = x orelse mem(e,xs); |
| 311 | + |
| 312 | +print ("\npowerset\n"); (* 4 points *) |
| 313 | +val x = powerset([1,2,3]); |
| 314 | +val lenRight = length x = 8; |
| 315 | +val score = score + (if lenRight andalso mem([],x) then 0.5 else 0.0); |
| 316 | +val score = score + (if lenRight andalso mem([1],x) then 0.5 else 0.0); |
| 317 | +val score = score + (if lenRight andalso mem([2],x) then 0.5 else 0.0); |
| 318 | +val score = score + (if lenRight andalso mem([3],x) then 0.5 else 0.0); |
| 319 | +val score = score + (if lenRight andalso mem([1,2],x) orelse mem([2,1],x) then 0.5 else 0.0); |
| 320 | +val score = score + (if lenRight andalso mem([1,3],x) orelse mem([3,1],x) then 0.5 else 0.0); |
| 321 | +val score = score + (if lenRight andalso mem([2,3],x) orelse mem([3,2],x) then 0.5 else 0.0); |
| 322 | +val score = score + (if lenRight andalso mem([1,2,3],x) orelse mem([1,3,2],x) orelse mem([2,1,3],x) orelse mem([2,3,1],x) orelse mem([3,2,1],x) orelse mem([3,1,2],x) then 0.5 else 0.0); |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | + |
| 340 | + |
0 commit comments