Lambda is an anonymous function
for example
/\ (x, y)
(x * x) + (y * y)
defition of a function becomes a value. This value can be assigned
to a variable
define f (x y ... z) e
can be achieved in a functional language by
f := /\ (x y .. z) e
( I use :=
as an assignment, denote a function)
The first example of a functional value
add := /\ (x) /\ (y) (x + y)
is an application of a function
add1 := add(1)
=> add1(4)
The above functions can be read: add has an argument (x)
returns a function that has an argument (n) which returns x + y
add1() has an argument (n) returns 1 + n
Please note that /\ (x y .. z) e
is an
expression.
when this expression is evaluate in the environment (env) it becomes a
closure
<< /\ (x y .. z) e, env >>
which is a value (distinct from the expression). env is a binding
of free variables
=> add1(2)
creates a closure
<< /\(y) (x + y), {x->1} >>
which is applied to argument 2 which will extend the environment to
{x->1, y -> 2 }
The next example shows another way to use "function value"
apply f to each element in the list L, returns a list
mapcar := /\ (f, L)
if L == nil return nil
else return cons( f (head(L)), mapcar ( f,
tail(L) ) )
for example if number? tests its argument returns Boolean
=> mapcar ( number?, [ 3, a, b, [5, 6]] )
=> [ True, nil, nil, nil ]
lastly, "function value:" can be used to capture the flow to control
it is call "continuation"
Homework: Read about continuation by searching for a function
named
call-with-current-continuation. (or call-cc. in short)
that is available in the language SCHEME.
last update 22 Feb 2024