Skip to content

Commit 8404d80

Browse files
Joydeep MitraJoydeep Mitra
Joydeep Mitra
authored and
Joydeep Mitra
committed
datatypes and more
1 parent 0cebd04 commit 8404d80

14 files changed

+60
-6
lines changed

backChange.sml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exception change;
2+
fun backChange(coinvals, 0) = []
3+
| backChange([], amount) = raise change
4+
| backChange(c::coinvals, amount) =
5+
if amount < 0 then raise change
6+
else c::backChange(c::coinvals, amount-c)
7+
handle change => backChange(coinvals, amount);
8+
9+
backChange([5,2], 16);

basics.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
10.0/2.0;
55
val five = 3 + 2;
66

7-
fun area(r) = 3.14 * r * r;
7+
fun area(r) = 3.14 * r * r;

conditionals.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fun toUpper(c) =
22
if #"a" <= c andalso c <= #"z"
33
then chr(ord(c) - 32)
4-
else c
4+
else c

funtypes.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fun double(n) = 2*n;
22
fun idem(n) = n;
3-
fun double1(n: real) = n*n;
3+
fun double1(n: real) = n*n;

map.sml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fun map f [] = []
2+
| map f (x::xs) = (f x)::map f xs;
3+
4+
map size ["Boston", "CS", "CS4400"];

polytypes.sml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
datatype 'a polyShape = Point of 'a * 'a
2+
| Line of ('a * 'a) * ('a * 'a)
3+
| Triangle of ('a * 'a) * ('a * 'a) * ('a * 'a);
4+
5+
Point((2),(3));
6+
Point((2.5),(3.0));

redeclare.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ fun area(r) = pi*r*r;
33
val pi = 12;
44
area(2.5);
55
fun show(x) = x;
6-
show(pi);
6+
show(pi);

shapes.sml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
datatype shape = Point of real * real
2+
| Line of (real * real) * (real * real)
3+
| Triangle of (real * real) * (real * real) * (real * real)
4+
5+
6+
fun showTriCood [] = []
7+
| showTriCood(Triangle(a,b,c)::ps) = (a,b,c)::showTriCood(ps)
8+
| showTriCood(p::ps) = showTriCood(ps)
9+
10+
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))];
11+
showTriCood(shapes);

strings.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ val i = ord(#"s");
44
val c = chr(i);
55

66
val s1 = String.sub(msg, 4);
7-
val s2 = String.substring(msg, 2, 6);
7+
val s2 = String.substring(msg, 2, 6);

summation.sml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fun summation f m =
2+
let fun sum (i,z) : real =
3+
if i=m then z else sum (i+1, z+(f i))
4+
in sum (0, 0.0) end;
5+
6+
7+
summation (fn k => real (k *k )) 10;

take.sml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
fun take ([], i) = []
2-
| take (x::xs, i) = x::take(xs, i-1);
2+
| take (x::xs, i) = if i > 0 then x::take(xs, i-1) else [];

treeops.sml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use "treetype.sml";
2+
3+
fun depth Lf = 0
4+
| depth (Br(v, t1, t2)) = 1 + Int.max(depth t1, depth t2);

treetrav.sml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use "treetype.sml";
2+
3+
fun preorder Lf = []
4+
| preorder (Br(v, t1, t2)) = [v] @ preorder t1 @ preorder t2;
5+
6+
fun inorder Lf = []
7+
| inorder (Br(v, t1, t2)) = inorder t1 @ [v] @ inorder t2;
8+
9+
fun postorder Lf = []
10+
| postorder (Br(v, t1, t2)) = postorder t1 @ postorder t2 @ [v];

treetype.sml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
datatype 'a tree = Lf | Br of 'a * 'a tree * 'a tree;
2+
3+
val birnam = Br(1, Br(2, Lf, Br(3,Br(4,Lf,Lf), Lf)), Lf);

0 commit comments

Comments
 (0)