Skip to content

Instantly share code, notes, and snippets.

@swatson555
Created September 16, 2025 01:47
Show Gist options
  • Select an option

  • Save swatson555/af8f6293353832ee75d11dcf7ad6f4d8 to your computer and use it in GitHub Desktop.

Select an option

Save swatson555/af8f6293353832ee75d11dcf7ad6f4d8 to your computer and use it in GitHub Desktop.

Datatypes

integers, reals, and strings

1;     (* => integer *)
1.0;   (* => real *)
"1.0"; (* => string *)

datatype with constructors

datatype list = nil | CONS int * list;

val p = new CONS of (1, new nil) (* p => cons pair *)
val n = new nil

n-tuples

(1, 2, 3, 4); (* => value of a 4-tuple *)

mutable arrays

[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) *)

single-argument, single-result functions

fun f x = x+1; (* f => value of function *)
fn x => x+1;   (* => value of function *)

Expressions

variables

let val a = 10
 in a
end

integer, real, and string literals

let val a = 1
    val b = 1.0
    val c = "1"
 in a
end

data constructors

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 expressions

case 10
  of 1 => 10
   | 2 => 5
   | _ => 0

n-tuple creation

(1, 2, 1.0, 3);

selection of fields from n-tuples

val xyz = (1, 2, 3);
#1 xyz;

function application

fun f x = x+1;
f 10;

function definition

fun f x = x+1
  | g y = f (y+1);
val f = fn x => x+1

mutually recursive function definition

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

primitive arithmetic and comparison operators

integer arithmetic

~1;
1 + 1;
1 - 1;
1 * 1;
1 / 1;

integer comparison

1 = 1;
1 <> 1;
1 < 1;
1 <= 1;
1 > 1;
1 >= 1;
1 in 10;

floating point arithmetic

1.0 fadd 1.0;
1.0 fsub 1.0;
1.0 fmul 1.0;
1.0 fdiv 1.0;

floating point comparison

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;

bitwise arithmetic

1 << 2;
1 >> 2;
1 orb 1;
1 andb 1;
1 xorb 1;
notb 1;

operators for manipulating references and arrays

references (objects)

val foo = makeref fn x => x+1;
!foo 10;
foo := 2;
boxed? foo;

unboxed references (integers)

val foo = makerefunboxed 10;
!foo;
foo unboxedassign 1;
boxed? foo;

arrays

val foo = [1,2,3,4];
foo subscript #1;
foo #1 update 10;
alength foo

unboxed arrays

val foo = u[1,2,3,4];
foo subscript #1;
foo #1 unboxedupdate 10;
alength foo

byte arrays / strings

val foo = b[1,2,3,4];
foo ordof #1;
foo #1 store 10;
slength foo;

simple exception handling

exception handlers

gethdlr;
sethdlr fn exn => 0;

raise / handle

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 / throw

callcc (fn k => 2 + throw k 1); (* => 1 *)

Other Expressions

Pattern matching, abstract types, and the module system (structures and functors) are omitted however some concepts from Standard ML are in this derivative.

top-level

val a = 10;
fun f x = x + a;
f a

let expressions

let val a = 10
    fun f x = x + a
 in f a
end

sequences

(1+1;
   2;
 10-1)

patterns

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment