Functional Prog: ML
Functions:

A function is a computation that maps an input domain to
an ouput range

In math, the function of a line of slope 2 and yintercept 3 is f(x)
= 2*x + 3

x is an identifier which takes on values in the domain of the
function  we call x a parameter of the function f

For a given value of x, f(x) is the value resulting from
computing its expression with x being replaced by its value 
e.g., if x is 2 , f(x) is 7  we also
just simply write f(2)=7
Functions in ML

functions are defined using the fun keyword:
fun square(x:real) = x * x;

because ML is strongly typed, we must tell ML the type of value x will
refer to  hence the need to use the x:real syntax

x is a parameter to the function square  it is an identifier
that refers to a value that the function will use to compute its result

ML responds by telling us square is a function "fn : real
> real"  what this means is that square takes a real value
and results in another real value. The arrow separates the input types
(the domain) of the function from the output type (the range)
Defining and Using functions

The definition of a function is done in the fun statement, as
above.

A function is used (we say called, or invoked), when it is
named in an expression, and values are used in place of each parameter
(the values can be existing identifiers, or expressions, but must always
be of the correct type).

The use of a function means that the values are used to evaluate the expression
in the body of the function, and the computed value is returned to the
expression containing the function use.
The scope of parameters

When we use x as a parameter to a function, that use of x
only applies to the body of the function

Any other "x" is not affected
Some examples are here
Identifier Scopes and Lifetimes
 When an identifier is created and assigned a value to refer to using
the val command, it has a certain area of use and a certain lifetime.
This area is called its scope.
 If we just type in a val statement into ML, that identifiervalue
binding has a scope of the rest of the ML session, or until the
identifier is reassigned a different value.
 A parameter of a function has a scope that is just the body of the
function. Its lifetime is just one execution of the function body.
The ML stack of identifiers (and function names)
 Think of an upward growing stack of identifiervalue bindings. Each
val statement adds a layer onto this stack.
 A reassignment of an existing identifier does not erase the old one.
It simply adds a new layer to the stack.
 When an indentifier is used in an expression, the stack is searched
from top to bottom. The first occurrence of the identifier is the
one whose value is used in the expression.
 When a function is defined, its name and body is added to the stack
as well. Very importantly, any identifier that is in the body that is not
a parameter is immediately searched for on the stack and located. It
is this identifiervalue binding that is used during function execution.
 Parameters are viewed as temporary additions to the identifier stack,
only there during the function execution. When the function begins (is
called), the value passed in as an argument is used to create an identifiervalue
binding for that parameter. When the function ends, that entry in the identifier
stack is erased.
 Functions can themselves be redefined! Just like identifiers,
they are simply added to the stack, and the old versions are not removed.
Previously defined functions using that function name refer to the old
binding, because they looked it up immediately when they were defined.
examples found here.
The let...in Statement: Defining temporary identifiers
 Function parameters are one type of temporary identifiervalue binding.
 Another is the let..in statement. It allows you to create
temporary identifiervalue 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 insidetoout 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
Function definitions,
and ML sessions
Tuples

ML has a data type that groups sets of values, called a tuple. A
tuple is an ordered set of data values; the values do not have to be the
same type. A tuple with three values is called a threetuple. In general,
N values makes an Ntuple.

A tuple is strongly typed, just like simple values are. Each value is a
specific type, and the type of the tuple is the ordered types of its values,
with a * in between.

This is the same notation for function types

In ML, a tuple is surround by parentheses, and the values are separated
by commas

A point on the Cartesian plane can be represented as a twotuple of two real numbers, like
(3.4,5.4), where the first number is the x coordinate and the second is
the y coordinate

A function can return a tuple as well  the body becomes a series of commaseparated
expressions, each computing the value for its place in the returned tuple.

Tuples can act as records of data  ("Doe,","John","123 Any St","Any
Town","TX",75005)