Useful Information for SML
|
type | values | operators |
---|---|---|
int | ...~2, ~1, 0, 1, 2... | ~, +, -, *, div, mod <, >, <=, >=, =, <> |
bool | true, false | not, and, or, andalso, orelse |
string | "abc", "z", etc | size, ^ |
real | 0.243, etc | ~, +, -, *, /,<, >, <=, >=, =, <>,sin, exp, sqrt, etc |
type converter functions:
real : int -> real
floor : real -> int
type | constructors | operations |
---|---|---|
list | ::, nil | hd, tl, =, pattern matches |
tuples | (a), (a,3), (a,3,"x") ... | pattern matches |
alternate form of list is
[a,b,c] <==> a::b::c::nil
[] <==> nil
- val block1 = rectangle (2.0, 5.0);
val block1 = rectangle (2.0,5.0) : shape
- val block2 = rectangle (3.5, 1.0);
val block2 = rectangle (3.5,1.0) : shape
- val disk1 = circle 4.7;
val disk1 = circle 4.7 : shape
- val disk2 = circle 8.0;
val disk2 = circle 8.0 : shape
- area block1 + area block2 + area disk1 + area disk2;
val it = 283.9594831 : real
We created two rectangles and two disks. Then we added up
their areas.
- val objects = [block1, block2, disk1, disk2];
val objects = [rectangle (2.0,5.0),rectangle (3.5,1.0),
circle 4.7,circle 8.0] : shape list
- map area objects;
val it = [10.0,3.5,69.3977231,201.06176] : real list
We made a list of our four objects, and mapped the area function
on the list, giving us a list of their areas.
- val disks = map circle [2.0, 6.5, 7.1, 3.3, 2.5];
val disks = [circle 2.0,circle 6.5,circle 7.1,circle 3.3,
circle 2.5] : shape list
We took a list of reals and mapped the circle function on it,
giving us a list of circles.