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

Anonymous Procedures and lambda

Scheme has a special form that is very special, called lambda. It creates a first-class procedure and returns a pointer to it.

For example, you can create a procedure that doubles its argument by evaluating the expression (lambda (x) (+ x x)). The second subform of the expression is a list of formal arguments, and the third subform is the body of the procedure.

lambda doesn't give a name to the procedure it creates--it just returns a pointer to the procedure object.

Actually, the procedure-defining variant of define is exactly equivalent to a variable-defining define, with a lambda expression as its initial value form.

For example,

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

is exactly equivalent to

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

In either case, we're creating a one-argument procedure, and we're also defining and binding a variable named double, and initializing its storage with a pointer to the procedure.

The procedure-defining syntax for define is just syntactic sugar--there's nothing you can do with it that you can't do with local variables and lambda. It's just a more convenient notation for the same thing.

==================================================================
This is the end of Hunk K.

TIME TO TRY IT OUT

At this point, you should go read Hunk L of the next chapter
and work through the examples using a running Scheme system.
Then return here and resume this chapter.
==================================================================

(Go to Hunk L, which starts at section Using First-Class, Higher-Order, and Anonymous Procedures (Hunk L).)


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