1; (* => integer *)
1.0; (* => real *)
"1.0"; (* => string *)
datatype list = nil | CONS int * list;
val p = new CONS of (1, new nil) (* p => cons pair *)
val n = new nil
(1, 2, 3, 4); (* => value of a 4-tuple *)
[1, 2, 3, 4]; (* => value of a 4-element array (contains any object) *)
u[1, 2, 3, 4]; (* => value of a 4-element array (contains only integers) *)
b[1, 2, 3, 4]; (* => value of a 4-element array (contains byte-sized integers) *)
fun f x = x+1; (* f => value of function *)
fn x => x+1; (* => value of function *)
let val a = 10
in a
end
let val a = 1
val b = 1.0
val c = "1"
in a
end
The book describes having a decon_??? and con_??? for each datatype.
datatype a = A | B of int * int * int
val b = new B of (1,2,3);
val c = new A;
get B of b;
get A of c; (* error *)
case 10
of 1 => 10
| 2 => 5
| _ => 0
(1, 2, 1.0, 3);
val xyz = (1, 2, 3);
#1 xyz;
fun f x = x+1;
f 10;
fun f x = x+1
| g y = f (y+1);
val f = fn x => x+1
The book describes using let val rec (even though there are no let expression)
fix f = fn x => (g 10) + 1
| g = fn x => x+1
in f 10
end
~1;
1 + 1;
1 - 1;
1 * 1;
1 / 1;
1 = 1;
1 <> 1;
1 < 1;
1 <= 1;
1 > 1;
1 >= 1;
1 in 10;
1.0 fadd 1.0;
1.0 fsub 1.0;
1.0 fmul 1.0;
1.0 fdiv 1.0;
1.0 feql 1.0;
1.0 fneq 1.0;
1.0 fgt 1.0;
1.0 fge 1.0;
1.0 flt 1.0;
1.0 fle 1.0;
1 << 2;
1 >> 2;
1 orb 1;
1 andb 1;
1 xorb 1;
notb 1;
val foo = makeref fn x => x+1;
!foo 10;
foo := 2;
boxed? foo;
val foo = makerefunboxed 10;
!foo;
foo unboxedassign 1;
boxed? foo;
val foo = [1,2,3,4];
foo subscript #1;
foo #1 update 10;
alength foo
val foo = u[1,2,3,4];
foo subscript #1;
foo #1 unboxedupdate 10;
alength foo
val foo = b[1,2,3,4];
foo ordof #1;
foo #1 store 10;
slength foo;
gethdlr;
sethdlr fn exn => 0;
let exception Fail
exception Err of string
val a = 10
val b = 20
in raise Fail;
a + b;
raise Err "unreachable code reached.";
end
handle Fail => 0
| Err (s) => s
callcc (fn k => 2 + throw k 1); (* => 1 *)
Pattern matching, abstract types, and the module system (structures and functors) are omitted however some concepts from Standard ML are in this derivative.
val a = 10;
fun f x = x + a;
f a
let val a = 10
fun f x = x + a
in f a
end
(1+1;
2;
10-1)
datatype a = A | B of int * int * int;
val xyz = new B of (1,2,3);
case xyz
of A => 10
| B (x,y,z) => x+y+z
(f 10)
handle Fail (s) => 0
| Div => 0