Go to the first, previous, next, last section, table of contents.

lambda

Recall that in Scheme, we can create anonymous (unnamed) procedures any time we want, using the lambda special form.

For example, suppose you want a procedure that doubles the values of the items in a list. You could do what we did before, and define a named double procedure, but if you only need to use the procedue in one place, it's easier to use an anonymous procedure created with lambda.

Instead of writing

(define (double x)
   (+ x x))

and then using it like this

   ...
   (map double mylist)
   ...

You can simply define it where it's used, using lambda.

   ...
   (map (lambda (x) (+ x x))
        mylist)
   ...

This can help avoid cluttering your code with lots of auxiliary procedures. (Don't overdo it, though--if a procedure is nontrivial, it's good to give it a name that reflects what it does.) This is very convenient when using higher-order procedures like map, or higher-order procedures you come up with for your own programs.

[ As we'll see in a little while, lambda has some very interesting properties that make it more useful than it might seem right now. ]

[ point out that variable arity works with lambda arg lists just like with define arg lists ]

Procedures are Closures

Scheme procedure's aren't really just pieces of code you can execute; they're closures.

A closure records not only what code a procedure must run, but also what environment it was created in. When you call it, that environment is restored before the actual code is executed. That is, the "current environment pointer" is set to point to that environment, and whenever the procedure references a variable, it will be looked up there.

This ensures that when a procedure executes, it sees the exact same variable bindings that were visible when it was created--it doesn't just remember variable names in its code, it remembers what storage each name referred to when it was created.

Since variable bindings are allocated on the heap, not on a stack, this allows procedures to remember binding environments even after the expressions that created those environments have been evaluated. For example, a closure created by a lambda inside a let will remember the let's variable bindings even after we've exited the let. As long as we have a pointer to the procedure (closure), the bindings it refers to are guaranteed to exist. (The garbage collector will not reclaim the procedure's storage, or the storage for the let bindings.)

We say that a procedure is closed in the environment where it is created. Technically, this is because a closure records the transitive closure of the "scoped in" relation; that is, it can see bindings created by the enclosing binding construct, bindings created by the one enclosing that, and so on until reaching the top level. Intuitively, you can also think of the set of bindings as closed when a procedure is created: bindings that are not lexically visible when the procedure is created are not visible when it runs. (Except for bindings created by the procedure itself when as it runs, that is--it can bind arguments, evaluate let expressions, etc.)

Here's an example that may clarify this, and show one way of taking advantage of it.

Suppose we type the following expression at the Scheme prompt, to be interpreted in a top-level environment:

Scheme> (let ((count 0))
           (lambda ()
              (set! count (+ count 1))
              count)))
#<proc ....># 

Notice that the let is not inside a procedure; Scheme variables don't have to be local to a procedure. In this case, count is just local to the let expression that binds it.

[ need picture here ]

Evaluating this let expression first creates a binding environment with a binding for count. The initial value of this binding is 0. In this environment, the lambda expression creates a closure. When executed, this procedure will increment the count, and then return its value. (Note that the procedure is not executed yet, however--it's just created so that it can be called to operate on the binding of count later.) This procedure, returned by the lambda expression, is also returned as the value of the let expression, because a let returns the value of its last body expression. The read-eval-print loop therefore prints a representation of the (anonymous) procedure.

Unfortunately, we didn't do anything with the value, like give it a name, so we can't refer to it anymore, and the garbage collector will just reclaim it. (OOPS!) Now suppose we want to do the same thing, but hold onto the closure so that we can do something with it, like calling it.

We'll bind a new variable my-counter, and use the above let expression to create a new environment and procedure, just like before.

Scheme> (define my-counter
                (let ((count 0))
                   (lambda ()
                      (set! count (+ count 1))
                      count))))
#void

(Notice that we're using plain variable definition syntax here--the only procedure we're creating is the value of the lambda expression, which we're storing in the binding of my-counter.

Now we have a top-level binding of my-counter, whose value is the procedure we created. It will remember the binding of count created by the let before evaluating the lambda expression.

(The crucial trick here relies on the fact that the let expression not only creates the local variable binding for count, but returns the value of the last expression in its body--i.e., the closure returned by the lambda expression. The pointer to the closure is passed along by the let to become the initial value for the binding of my-counter.)

The procedure keeps a pointer to the environment created by the let, which in turn has a pointer to the top-level environment, thus:

[ should simplify this picture and use it earlier, for the simpler example where we don't keep a pointer to the closure. Should show the envt register pointing to the let envt at the moment the closure is created. ]

                    [envt]
         +-->+------------+-----+             
         |   |      car   |  *--+--> ...    
         |   +------------+-----+            
         |   |      cons  |  *--+--> ...   
         |   +------------+-----+        
         |   |        *         |  
         |            *         
         |   |        *         |   
         |   +------------+-----+
         |   | my-counter |  *--+------------+
         |   +------------+-----+            |
         |                  /|\              |
         |                   |               |
         |     [envt]        |               |
         |   +------------+--+--+            |
         |   |   [scope]  |  *  |            |
         |   +------------+-----+            |
         |   |      count |  *--+-->0        |
         |   +------------+-----+           \|/
         |            /|\                [closure]
         |             |                +---------+
         |             +----------------+----*    |
         |                              +---------+ 
         |                              |    *    |
         |                              +----+----+ 
         |                                   |
         |                                  \|/
         |                                 [code]
         |                          +--------------------+
     +---+---+                      | (set! count        |
envt |   *   |                      |       (+ count 1)) |
     +-------+                      | count              |
                                    +--------------------+

Now if we call the procedure my-counter, it will execute in its own "captured" environment (created by the let). It will increment the binding of count in that environment, and return the result. The environment will continue to exist as long as the procedure does, and will store the latest value until next time my-counter is called:

Scheme>(my-counter)
1
Scheme>(my-counter)
2
Scheme>(my-counter)
3

Notice that if we evaluate the whole let form again, we will get a new let environment, with a new binding of count, and a new procedure that will increment and return its count value--in effect, each procedure has its own little piece of state which only it can see (because only it was created in that particular environment). Each one remembers which piece of storage count referred to when it was created, and operates on that particular piece of storage.

If we want, we can define a procedure that will create new environments, and new procedures that capture those environments--we can generate new counter procedures just by calling that "higher-order" procedure. (Recall that a higher-order procedure is just a procedure that manipulates other procedures. In this case, we're making a procedure that generates procedures.)

Each time make-counter is called, it will execute a let, creating an environment, and inside that it will use lambda to create a counter procedure.

Scheme> (define (make-counter)
           ;; bind count and create a new procedure that will (when
           ;; called) increment that binding and return its value
           (let ((count 0))
              (lambda ()
                 (set! count (+ count 1))
                 count)))
make-counter

(Note that here we're using procedure-definition syntax.)

Each of the resulting procedures will have its own captured count variable, and keep it independently of the other procedures.

Make sure you understand that the above procedure definition could have used an explicit lambda to create the procedure make-counter, rather than the special procedure definition syntax:

Scheme> (define make-counter
                ;; create a procedure that will bind count and
                ;; return a new procedure that will increment that
                ;; binding and return its value
                (lambda ()
                   (let ((count 0))
                      (lambda ()
                         (set! count (+ count 1))
                         count)))

You may actually find this easier to understand, because it shows you exactly what's going on: binding make-counter and creating a procedure (with the outer lambda) that when called, will evaluate a let to create an environment, and a lambda (the inner one) to create a new procedure that captures that particular environment.)

Now we'll call the procedure created by the above definition, three times, and each time it will create a new procedure:

Scheme> (define c1 (make-counter))
C1
Scheme> (define c2 (make-counter))
C2
Scheme> (define c3 (make-counter))
C3

Now we'll call those procedures and look at their return values, to illustrate that they're independent counters:

Scheme> (c1)
1
Scheme> (c1)
2
Scheme> (c2)
1
Scheme> (c2)
2
Scheme> (c1)
3
Scheme> (c1)
4
Scheme> (c3)
1

Neat, huh? The combination of block structure (local environments) with first-class procedures (closures), allows us to associate state with procedures. Garbage collection makes this very convenient, because we know that the environments will hang around as long as the procedures do.

This example shows that we can use closures to create private variable bindings. Notice that once we've exited a let, the variables aren't visible anymore. But if we call a closure that was created there, they become visible again---to that closure only. The only way to operate on a variable binding after it has gone out of scope is to call a procedure that was created while it was in scope. This means that once a binding construct has been exited, the set of procedures that can operate on the bindings it creates is fixed. As I'll show later in this chapter, we can use this to structure programs and make sure that things don't interact when they're not supposed to.

If you're familiar with object-oriented programming, you may notice a resemblance between closures and "objects" in the object-oriented sense. A closure associates data with a procedure, where an object associates data with multiple procedures. After we get to object-oriented programming, we'll explain how object-oriented programming facilities can be implemented in Scheme using closures.

If you're familiar with graphical user interface systems, you may notice that GUI's often use "callbacks," which are procedures that are executed in response to user input events like button clicks and menu selections, and do something application-specific. (The application "registers" callback procedures with the GUI system, which then calls them when the user clicks on the specified buttons.) Closures make excellent GUI callback procedures, because the application can create a closure for a specific context by capturing variable bindings, to customize the behavior of the procedure.

Since argument variables are just local variables that get their initial values in a special way, we can use argument variables in much the same way as let variables.

Here's a new version of make-counter, which takes an argument that gives the initial value for a counter--it doesn't have to start at zero.

(define (make-counter count)
   ;; return a new procedure to increment argument variable
   ;; count and return its value
   (lambda ()
      (set! count (+ count 1))
      count))

Here we're using procedure-definition syntax, so we're creating a procedure of one argument count.

Whenever the procedure is called, count will be bound (once) and initialized to whatever value we give as an argument to make-counter. Then the lambda expression will be evaluted to create a new procedure that captures that binding of count.

(The argument variable count is bound to a fresh piece of storage when the procedure is entered, and we can "capture" that binding by creating a closure in its scope. As with a let variable, we get a different piece of storage each time we call make-counter.)

For this kind of counter, we'd probably rather return the old value of the counter, rather than the new one, each time we increment it. To do that, we can put a let inside the lambda expression, to hold onto the old value

(define (make-counter count)
   ;; create a procedure that 
   (lambda ()
      (let ((value count))         ;; hang onto value of count
         (set! count (+ count 1))  ;; increment count
         value)))                  ;; return previous value

Lambda is cheap, and Closures are Fast

It may seem that lambda is an expensive operation--after all, it creates procedure objects on the fly. At first glance, you might think that executing lambda would require a call to the compiler each time. This is not the case, though, and lambda is actually a fairly cheap constant-time operation.

Notice that the procedure part of a lambda expression is known at compile time--each time the lambda is executed at run time, it will create a new closure, and may capture a new environment, but the expression closed in that environment is determined solely by the body of the lambda expression. A compiler for Scheme will therefore compile the code for all of the closures created by a particular lambda expression, when it compiles the enclosing procedure. So, for example, when our example procedure make-counter is compiled, the compiler will also compile the code for the lambda body. This code will be kept around for use by make-counter.

The actual run-time code for lambda just fetches the address of the code, and the current environment pointer, and puts them in a creates a new closure object on the heap. lambda is therefore about as fast as cons---all that's really happening is the creation of the closure object itself, not anything expensive like calling the compiler at run-time.

(At this point, some people who are really concerned with efficiency may be wondering if Scheme is slow because variable bindings are allocated on the heap rather than on a stack, or in registers. Don't worry much about this--a good Scheme compiler can actually avoid heap-allocating environments when no closures are created in their scope, and can register-allocate most variables, as other compilers do.(11))


Go to the first, previous, next, last section, table of contents.