Standard ML Programming/Expressions

Tokens
A Standard ML program consists of a sequence of "tokens"; these may be thought of as the "words" of the language. Some of the most common token types are:

Arithmetic expressions
Arithmetic expressions are similar to those in many other languages:

3 + 4 3.0 / 4.0 (2 - 3) * 6

However, a few points bear note:


 * Unary negation is expressed using the tilde ~ rather than the hyphen -; the latter is used only for binary subtraction. For example, three minus negative-two is written 3 - ~2 or 3 - ~ 2.
 * Though the operators are "overloaded" to support multiple numeric types — for example, both 3 + 4 and 3.0 + 4.0 are valid (the former having type, the latter type  ) — there is no implicit promotion between types. Therefore, an expression such as 3 + 4.0 is not valid; one must write either 3.0 + 4.0, or <tt>real 3 + 4.0</tt> (using the basis function <tt>real</tt> to convert <tt>3</tt> to <tt>3.0</tt>).
 * Integer division is expressed using the special operator <tt>div</tt> rather than the solidus <tt>/</tt>; the latter is used only for real-number division. (And since there is no implicit promotion between types, an expression such as <tt>3 / 4</tt> is not valid; one must write either <tt>3.0 / 4.0</tt> or <tt>real 3 / real 4</tt>.) There is also a modulus operator <tt>mod</tt>; for example, seventeen divided by five is three-remainder-two, so <tt>17 div 5</tt> is <tt>3</tt> and <tt>17 mod 5</tt> is <tt>2</tt>. (More generally, if q is a positive integer, then <tt>d = p div q</tt> and <tt>m = p mod q</tt> are integers such that <tt>p = d * q + m</tt>, <tt>m >= 0</tt>, and <tt>m < q</tt>. If q is a negative integer, then <tt>p = d * q + m</tt>, <tt>m <= 0</tt>, and <tt>m > q</tt>.)

Function calls
Once a function has been declared:

fun triple n = 3 * n

it is called simply by following the function-name with an argument:

val twelve = triple 4

In the general case, parentheses are not necessary, but they are frequently necessary for grouping. Also, as we saw in the chapter on types, tuples are constructed using parentheses, and it is not uncommon to construct a tuple as a function argument:

fun diff (a, b) = a - b val six = diff (9, 3)

Function calls have very high precedence, higher than any infix operator; so,  means , which is 2, rather than  , which is 0. Also, they are left-associative;  means   (where   takes   as its argument and returns a function that accepts   as its argument), not.

Infix function calls
A binary function — that is, a function whose parameter type is a 2-tuple type — can be turned into an infix operator:

fun minus (a, b) = a - b val three = minus (5, 2) infix minus val seven = 4 minus ~3 val two = op minus (20, 18)

An infix operator can have any precedence level from 0 to 9, 0 (the default) being the lowest precedence, 9 being the highest. The Standard Basis provides these built-in infix specifications:

infix 7  * / div mod infix 6  + - ^ infixr 5 :: @ infix 4  = <> > >= < <= infix 3  := o infix 0  before

Notice that in the third line, <tt>::</tt> and <tt>@</tt> are made infix using <tt>infixr</tt> rather than <tt>infix</tt>. This makes them right-associative rather than left-associative; whereas <tt>3 - 4 - 5</tt> means <tt>(3 - 4) - 5</tt>, <tt>3 :: 4 :: nil</tt> means <tt>3 :: (4 :: nil)</tt>.

An identifier can actually be declared infix even before it refers to a specific function:

infix pow fun x pow y = Math.pow (x, y) val eight = 2.0 pow 3.0

Note that in this case, the infix notation is already used in declaring the function. Another way of doing this is by using <tt>op</tt> in the function declaration, which works regardless if the function has already been declared infix or not:

fun op pow (x, y) = Math.pow (x, y)

The preferred style is usually to not do this, but it can be useful if the declaration happens in a setting where it is not certain whether the function has been declared infix or not, or when <tt>use</tt> might be called on a file containing such a function declaration more than once, where the function is declared as infix after its definition. It can also be a way of signalling that the function might be declared as infix later on, possibly in another file, in which case it can be useful to ensure that the order of parsing the files does not matter.

Comparisons
As we saw in the chapter on types, the  (boolean) type has two values,   and. We also saw the built-in polymorphic equality operator, of type. Closely related is the inequality operator, also of type  , which returns   when   would return  , and vice versa.

The  (less than),   (greater than),   (less than or equal to), and   (greater than or equal to) operators are overloaded to be usable with a variety of numeric, character, and string types (but as with the arithmetic operators, both operands must have the same type).

Operations on booleans
Three main functions operate on boolean values:


 * The function, of type  , maps   to   and vice versa.
 * The infix operator, of type  , maps   to  , and all other possibilities to  . This is know as a short circuit operator; if its first operand is  , then it will return   without evaluating its second operand.
 * The infix operator, of type  , maps   to  , and all other possibilities to  . Like   it is a shortcutting operator, but in the opposite direction: if its first operand is  , then it will return   without even evaluating its second operand.

Conditional expressions
One major use of boolean values is in conditional expressions. An expression of this form:

if boolean_expression then expression_if_true else expression_if_false

evaluates to the result of  if   evaluates to , and to the result of   if   evaluates to. As with the shortcutting operators, the unneeded expression is not evaluated. This allows conditional expressions to be used in creating recursive functions:

fun factorial x = if x = 0 then 1 else x * (factorial (x - 1))

It also allows for conditional side effects:

if x = 0 then print "x = 0" else print "x <> 0"

Note that, since conditional expressions return a value, the "then" and "else" branches are both required to be present, and to have the same type, though they do not have to be particularly meaningful:

if x = 0 then print "x = 0" else

Case expressions and pattern-matching
Functions may be composed of one or more rules. A rule consists of its function-name, an argument pattern and its expression. When the function is called the argument values will be matched to the patterns in top down order. Functions using pattern-matching are very similar to case expressions. In fact you can transform any case construct into a function. For example this snippet

case compare(a,b) of GREATER => 1 LESS => 2 EQUAL => 3

is semantically equal to

fun case_example_function GREATER = 1 | case_example_function LESS = 2 | case_example_function EQUAL = 3; case_example_function compare(a,b);

The first matching rule will get its expression evaluated and returned. That means if the patterns are overlapping (multiple patterns match a given argument value) one must keep in mind that only the first matching rule gets evaluated.

fun list_size (nil) = 0 | list_size (_::xs) = 1 + str_len xs;

The functions pattern is called exhaustive if there is a matching pattern for all legal argument values. The following example is non-exhaustive.

fun list_size ([a]) = 1 | list_size ([a,b]) = 2;

Any empty list or lists of size>2 will cause a -exception. To make it exhaustive one might add a few patterns.

fun list_size ([a]) = 1 | list_size ([a,b]) = 2 | list_size (nil) = 0 | list_size (_) = 0;

Lists of size>2 will return 0 which does not make a lot of sense but the functions pattern is now exhaustive.

Exceptions
Exceptions are used to abort evaluation. There are several built in exceptions that can be thrown using the <tt>raise</tt> keyword, and you can define your own using the <tt>exception</tt> keyword. Exceptions can have messages attached to them by writing <tt>of</tt> in the declaration and they are used much like datatype constructors. An exception can be caught using the <tt>handle</tt> keyword. Example:

exception Exc of string; fun divide (_, 0) = raise Exc("Cannot divide by zero") | divide (num, den) = num div den val zero_or_infinity = divide (0, 0) handle Exc msg => (print (msg ^ "\n"); 0)

This will print "Cannot divide by zero" and evaluate zero_or_infinity to 0 thanks to the <tt>handle</tt> clause.

Built in exceptions include Empty, Domain and Fail(msg).

Lambda expressions
A lambda expression is an expression that can be evaluated into a function without the function being bound to an identifier, a.k.a. an anonymous function, function constant or a function literal. Such a function can be defined in Standard ML using the <tt>fn</tt> keyword. This is particularly useful for higher order functions, i.e. functions that take other functions as arguments, such as the built in <tt>map</tt> and <tt>foldl</tt> functions. Instead of writing:

fun add_one x = x + 1 val incremented = map add_one [1, 2, 3]

You could simply write

val incremented = map (fn x => x + 1) [1, 2, 3]

Note that the <tt>=></tt> operator is used instead of <tt>=</tt>. The <tt>fn</tt> keyword can be used in place of <tt>fun</tt>, including pattern matching and even recursion (if the <tt>rec</tt> keyword is used):

val rec fact = fn 1 => 1 | n => if n > 1 then n * fact(n - 1) else raise Domain

Be aware that it is often considered better style to use the <tt>fun</tt> keyword.