diff --git a/exercises/compareTime.sml b/exercises/compareTime.sml index 4489c57..31dfa28 100644 --- a/exercises/compareTime.sml +++ b/exercises/compareTime.sml @@ -1,4 +1,10 @@ -fun timeBefore((h1, m1, "AM"), (h2, m2, "AM")) = false; +fun toElapsedMinutes(h1, m1, t1) = + h1 * 60 + + m1 + + (if t1 = "PM" then 12*60 else 0); + +fun timeBefore((h1, m1, t1), (h2, m2, t2)) = + toElapsedMinutes(h1, m1, t1) < toElapsedMinutes(h2, m2, t2); timeBefore((1, 59, "AM"), (2, 40, "PM")) = true; timeBefore((1, 59, "PM"), (2, 40, "PM")) = true; diff --git a/exercises/eqTri.sml b/exercises/eqTri.sml index e042b83..f630d27 100644 --- a/exercises/eqTri.sml +++ b/exercises/eqTri.sml @@ -2,10 +2,27 @@ datatype shape = Point of real * real | Line of (real * real) * (real * real) | Triangle of (real * real) * (real * real) * (real * real) +fun close (a:real, b:real) = (a - b) < 0.0001 andalso (a - b) > ~0.0001 + +fun distance ((ax: real, ay: real), (bx: real, by: real)) = + Math.sqrt ( (ax - bx) * (ax - bx) + (ay - by) * (ay - by) ); + +fun isEqTri (Point (_)) = false + | isEqTri (Line (_)) = false + | isEqTri (Triangle (a, b, c)) = + (close (distance (a, b), distance (b, c))) andalso + (close (distance (a, b), distance (a, c))); + + fun showTriCood [] = [] -| showTriCood(Triangle(a,b,c)::ps) = (a,b,c)::showTriCood(ps) -| showTriCood(p::ps) = showTriCood(ps) + | showTriCood(Triangle(a,b,c)::ps) = (a,b,c)::showTriCood(ps) + | showTriCood(p::ps) = showTriCood(ps) + +fun containsEqTri l = List.exists isEqTri l; val shapes = [Line((2.2,3.5),(4.1,5.0)), Point(4.5,2.0), Triangle((21.0, 4.5),(15.5, 21.1),(10.5, 10.2))]; -showTriCood(shapes); \ No newline at end of file +showTriCood(shapes); +val hasEqTri = [Line((2.2,3.5),(4.1,5.0)), Point(4.5,2.0), Triangle((0.0, 0.0),(0.5, 0.866025404),(1.0, 0.0))]; +containsEqTri(shapes); +containsEqTri(hasEqTri); \ No newline at end of file diff --git a/exercises/fib.sml b/exercises/fib.sml index 705bab2..316968e 100644 --- a/exercises/fib.sml +++ b/exercises/fib.sml @@ -1,7 +1,12 @@ +fun fibt(0, n1, n2) = n1 + | fibt(count, n1, n2) = fibt(count - 1, n1 + n2, n1); + fun fib(0) = 0 -| fib(1) = 1 -| fib(n) = fib(n-2) + fib(n-1); + | fib(n) = fibt(n - 1, 1, 0); + fib(10) = 55; fib(8) = 21; -fib(2) = 1; \ No newline at end of file +fib(2) = 1; +fib(50) = 12586269025; +e \ No newline at end of file diff --git a/exercises/makeChange.sml b/exercises/makeChange.sml index 1441dd9..1379db4 100644 --- a/exercises/makeChange.sml +++ b/exercises/makeChange.sml @@ -1,9 +1,10 @@ -fun allChange(coins, coinvals, 0) = [coins] -| allChange(coins, [], amount) = [] -| allChange(coins, c::coinvals, amount) = - if amount < 0 then [] - else allChange(c::coins, c::coinvals, amount-c) @ - allChange(coins, coinvals, amount); +fun allChange(coins, coinvals, 0, res) = coins :: res + | allChange(coins, [], amount, res) = res + | allChange(coins, c::coinvals, amount, res) = + if amount < 0 then res + else allChange(c::coins, c::coinvals, amount-c, allChange(coins, coinvals, amount, res)); -allChange([], [5,2], 16); -allChange([], [25,10,2,1], 43); \ No newline at end of file +allChange([], [5,2], 16, []); +allChange([], [25,10,2,1], 43, []); + +e \ No newline at end of file diff --git a/exercises/max.sml b/exercises/max.sml index bed83eb..dfed933 100644 --- a/exercises/max.sml +++ b/exercises/max.sml @@ -1,5 +1,12 @@ -fun maxm [m] = m -| maxm(m::n::ns) = if m > n then maxm(m::ns) else maxm(n::ns); +fun max2 (i1, i2) = if i1 > i2 then i1 else i2; + +fun maxmt (sofar, []) = sofar + | maxmt (sofar, h::tail) = maxmt(max2(sofar, h), tail) + +fun maxm [] = NONE + | maxm (m::ns) = SOME(maxmt(m, ns)); maxm([43,25,2,4,2434]); -maxm([]); \ No newline at end of file +maxm([30, 41, 22, ~12, 0, 3]); +maxm([]); +e \ No newline at end of file diff --git a/exercises/prod.sml b/exercises/prod.sml index 8987833..95d123b 100644 --- a/exercises/prod.sml +++ b/exercises/prod.sml @@ -1,6 +1,9 @@ (* Make tail recursive *) +fun prodt (total, []) = total + | prodt (total, h::tail) = prodt(total * h, tail); fun prod [] = 1 -| prod (n::ns) = n * (prod ns); + | prod (n::ns) = prodt(n, ns); prod([2,3,4,5]) = 120; -prod([]) = 1; \ No newline at end of file +prod([]) = 1; +e \ No newline at end of file diff --git a/exercises/treetav.sml b/exercises/treetav.sml index 0c803a7..9b0a34c 100644 --- a/exercises/treetav.sml +++ b/exercises/treetav.sml @@ -1,3 +1,21 @@ datatype 'a tree = Lf | Br of 'a * 'a tree * 'a tree; -val birnam = Br(1, Br(2, Lf, Br(3,Br(4,Lf,Lf), Lf)), Lf); \ No newline at end of file +fun treedepth Lf = 0 + | treedepth (Br (_, l, r)) = 1 + Int.max(treedepth l, treedepth r); + +fun preorder (Lf, res) = res + | preorder (Br(v, t1, t2), res) = v::(preorder (t1, preorder(t2, res))); + +fun inorder (Lf, res) = res + | inorder (Br(v, t1, t2), res) = (inorder (t1, v::inorder(t2, res))); + +fun postorder (Lf, res) = res + | postorder (Br(v, t1, t2), res) = (postorder (t1, postorder(t2, v::res))); + +val birnam = Br(1, Br(2, Lf, Br(3,Br(4,Lf,Lf), Lf)), Lf); +treedepth(birnam); +preorder(birnam, []); +inorder(birnam, []); +postorder(birnam, []); + +e \ No newline at end of file diff --git a/exercises/validateDayMonth.sml b/exercises/validateDayMonth.sml index 609af5a..5d11839 100644 --- a/exercises/validateDayMonth.sml +++ b/exercises/validateDayMonth.sml @@ -1,2 +1,19 @@ (* Complete the expression *) -fun isValidDate(d, m) = true; \ No newline at end of file +fun isValidDate(d, m) = + d >= 1 andalso + d <= ( + case m of + "January" => 31 + | "February" => 28 + | "March" => 31 + | "April" => 30 + | "May" => 31 + | "June" => 30 + | "July" => 31 + | "August" => 31 + | "September" => 30 + | "October" => 31 + | "November" => 30 + | "December" => 31 + | _ => 0 + )