Conditionals Recursion Lists

Special forms in SML

Special forms in SML include

(1) Functions fn x => x+5 is the equivalent of (lambda (x) (+ x 5))

(2) Definitions

is the equivalent of (define x (+ 34 7))

You can define non-recursive functions this way

But you can't use this to define recursive functions, for which you must use the fun or letrec constructs introduced later.

(3) Tuples. A list of (at least two) expressions separated by commas and enclosed in parentheses

is called a tuple. A 2-tuple is called a pair.

Note that various syntactic variants such as x+y are not special forms. In SML x+y means "apply the function + to the pair (x,y).

The if ...then...else construction in SML

SML provides conditional expressions in which a condition (an expression which must have a boolean value) or conditions are used to choose which of a number of other expressions is to be evaluated. The simplest form of conditional in SML is the special form:

It evaluates condition. If the condition evaluates to true then expr1 is evaluated and is the value of the whole conditional expression, otherwise expr2 is evaluated and is the value of the whole conditional expression.

Note that SML differs from Scheme in that condition must be a boolean expression.

A list in SML is written in square brackets. All the objects in the list must be of the same type.

[1,2,3]

fun average list = sum(list) / length(list);