Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sean Seibel exercises #3

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion exercises/compareTime.sml
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
23 changes: 20 additions & 3 deletions exercises/eqTri.sml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
11 changes: 8 additions & 3 deletions exercises/fib.sml
Original file line number Diff line number Diff line change
@@ -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;
fib(2) = 1;
fib(50) = 12586269025;
e
17 changes: 9 additions & 8 deletions exercises/makeChange.sml
Original file line number Diff line number Diff line change
@@ -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);
allChange([], [5,2], 16, []);
allChange([], [25,10,2,1], 43, []);

e
13 changes: 10 additions & 3 deletions exercises/max.sml
Original file line number Diff line number Diff line change
@@ -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([]);
maxm([30, 41, 22, ~12, 0, 3]);
maxm([]);
e
7 changes: 5 additions & 2 deletions exercises/prod.sml
Original file line number Diff line number Diff line change
@@ -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;
prod([]) = 1;
e
20 changes: 19 additions & 1 deletion exercises/treetav.sml
Original file line number Diff line number Diff line change
@@ -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);
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
19 changes: 18 additions & 1 deletion exercises/validateDayMonth.sml
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
(* Complete the expression *)
fun isValidDate(d, m) = true;
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
)