Constructing Functions Using Lambda
In using pisum/2
as in the section Functions as Arguments, it seems terribly awkward to have to define trivial flet
functions such as piterm/1
and pinext/1
just so we can use them as arguments to our higherorder function. Rather than define pinext/1
and piterm/1
, it would be more convenient to have a way to directly specify "the function that returns its input incremented by 4" and "the function that returns the reciprocal of its input times its input plus 2." We can do this by introducing the special form lambda
, which creates functions. Using lambda we can describe what we want as
(lambda (x) (+ x 4))
and
(lambda (x) (/ 1.0 (* x (+ x 2))))
Then our pisum/2
function can be expressed (without defining any auxiliary functions in an flet
) as
(defun pisum (a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
Again using lambda
, we can write the integral/4
function without having to define the auxiliary function adddx/1
:
(defun integral (f a b dx)
(* (sum f
(+ a (/ dx 2.0))
(lambda (x) (+ x dx))
b)
dx))
In general, lambda is used to create functions in the same way as defun
, except that no name is specified for the function:
(lambda (<formalparameters>) <body>)
The resulting function is just as much a function as one that is created using define. The only difference is that it has not been associated with any name in the environment.
We can read a lambda expression as follows:
(lambda (x) (+ x 4))
^ ^ ^ ^ ^
    
the function of an argument x that adds x and 4
Like any expression that has a function as its value, a lambda
expression can be used as the operator in a combination such as
> (apply (lambda (x y z) (+ x y (square z))) '(1 2 3))
12
or, more generally, in any context where we would normally use a function name.^{1} For example, if we defined the lambda
expression above as a function
(defun addsq (x y z)
(+ x y (square z)))
we would apply it the same way as we did the lambda
expression:
> (apply #'addsq/3 '(1 2 3))
12
Using let
to create local variables
Another use of lambda
is in creating local variables. We often need local variables in our functions other than those that have been bound as formal parameters. For example, suppose we wish to compute the function
$$ \begin{align} f(x, y) = x(1 + xy)^2 + y(1 y) + (1 + xy)(1  y) \end{align} $$
which we could also express as
$$ \begin{align} a = & \ 1 + xy \ b = & \ 1  y \ f(x, y) = & \ ra^2 + yb + ab. \end{align} $$
In writing a function to compute $$f$$, we would like to include as local variables not only $$x$$ and $$y$$ but also the names of intermediate quantities like $$a$$ and $$b$$. One way to accomplish this is to use an auxiliary function to bind the local variables:
(defun f (x y)
(flet ((fhelper (a b)
(+ (* x (square a))
(* y b)
(* a b))))
(fhelper (+ 1 (* x y))
( 1 y))))
Of course, we could use a lambda
expression to specify an anonymous function for binding our local variables. The body of f
then becomes a single call to that function:
(defun f (x y)
(funcall
(lambda (a b)
(+ (* x (square a))
(* y b)
(* a b)))
(+ 1 (* x y))
( 1 y)))
This construct is so useful that there is a special form called let
to make its use more convenient. Using let
, the f/2
function could be written as
(defun f (x y)
(let ((a (+ 1 (* x y)))
(b ( 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))
The general form of a let
expression is
(let ((<var1> <exp1>)
(<var2> <exp2>)
...
(<varn> <expn>))
<body>)
which can be thought of as saying
 Let
<var1>
have the value<exp1>
and  Let
<var2>
have the value<exp2>
and  ... and
 Let
<varn>
have the value<expn>
 All in the context of
<body>
The first part of the let
expression is a list of nameexpression pairs. When the let
is evaluated, each name is associated with the value of the corresponding expression. The body of the let
is evaluated with these names bound as local variables. The way this happens is that the let
expression is interpreted as an alternate syntax for
(funcall
(lambda (<var1> ... <varn>)
<body>)
<exp1>
...
<expn>)
No new mechanism is required in the interpreter in order to provide local variables. A let
expression is simply syntactic sugar for the underlying lambda
application.
We can see from this equivalence that the scope of a variable specified by a let
expression is the body of the let
. This implies that:

let
allows one to bind variables as locally as possible to where they are to be used. For example, if the value ofx
is 5, the value of the expression(+ (let ((x 3)) (+ x (* x 10))) x)
is 38. Here, the
x
in the body of thelet
is 3, so the value of the let expression is 33. On the other hand, thex
that is the second argument to the outermost+
is still 5. 
The variables' values are computed outside the
let
. This matters when the expressions that provide the values for the local variables depend upon variables having the same names as the local variables themselves. For example, if the value ofx
is 2, the expression(let ((x 3) (y (+ x 2))) (* x y))
will have the value 12 because, inside the body of the
let
,x
will be 3 andy
will be 4 (which is the outerx
plus 2).
It would be clearer and less intimidating to people learning Lisp if a name more obvious than lambda
, such as makefunction
, were used. But the convention is firmly entrenched. The notation is adopted from the $$\lambda$$ calculus, a mathematical formalism introduced by the mathematical logician Alonzo Church (1941). Church developed the $$\lambda$$ calculus to provide a rigorous foundation for studying the notions of function and function application. The $$\lambda$$ calculus has become a basic tool for mathematical investigations of the semantics of programming languages.