ML Lecture 6
The let...in Statement: Defining temporary identifiers
- Function parameters are one type of temporary identifier-value binding.
- Another is the let..in statement. It allows you to create
temporary identifier-value bindings for use in an expression. Its form
is
let
val id1 = value;
val id2 = value;
in
some expression
end;
The identifiers id1, id2, ... are bound to the values
specified (these can be expressions, of course), and then the expression
after in is evaluated, and the resulting value of this expression is the
resulting value of the let...in statement. After that expression
is evaluated the bindings created for id1, id2, ... are
erased.
The let statement allows you to break apart a computation into separate
steps
- The examples used used in class are in here,
the file let.sml, and the ML session is here.
- We used functions to break apart the computation as well, but then
the expressions became complex because of the inside-to-out order of evaluation.
The first step would be a function innermost in the expression, then the
next outermost part of the expression, and on until the whole expression
was evaluated. This is shown in the example function circle_area1 in
the examples.
- The let statement lets us put each of the computation steps
in a separate expression, and assign that intermediate step's value to
a new identifier. So it is more like how we think when we solve the problem,
and it prevents individual expressions from getting too complex. The example
function circle_area2 in the examples uses the let statement to
make this computation clearer.
- the final function in the examples is rect_area, and just
shows another use of the let statement
Slides in class: function definitions,
and ML sessions