sicp.io
1.3.2 · Constructing procedures with lambda

Lambda creates a procedure before any argument arrives.

Compare named and anonymous procedures, apply a lambda directly, and follow lexical bindings through nested procedure construction.

Guiding question

What value does a lambda expression create, and which bindings will its body use when that value is later applied?

  • Read lambda as an expression that produces a procedure value
  • Relate define procedure shorthand to a named lambda binding
  • Apply an anonymous procedure without assigning it a permanent name
  • Follow nested parameter scopes through lexical lookup
  • Pass a freshly created procedure as an ordinary argument value

The definition shorthand (define (square x) ...) binds a name to a procedure. Writing (define square-lambda (lambda (x) ...)) makes the two stages explicit: lambda creates the procedure value, and define binds a name to it. A lambda can also be applied immediately, so the two-argument sum-of-squares procedure needs no lasting top-level name.

In the nested example, the outer lambda binds x to 3 and the inner lambda binds y to 4. The inner body finds y in its own call and x in the surrounding lexical environment; the global x remains 100. The final expression passes a newly created square procedure into another lambda, showing that procedure construction, binding, passing, and application all use the ordinary value model.

SICP code202 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns (36 36 25). The second returns (100 7 100).

    Trace focus

    Separate each lambda evaluation, which creates a procedure value, from each later application. In the nested program, locate the inner lookup of x in the enclosing call environment and y in the current call. Confirm that the global x binding is not mutated.

    Try it yourself

    Change the program and compare the result.

    Rewrite a two-binding let expression as an immediate application of a two-parameter lambda. Then pass a lambda that triples its input into a procedure that applies its argument twice.

    Show hint

    A let with parallel bindings can be modeled as ((lambda (name ...) body) value ...). Keep procedure creation separate from each application.

    Complete this lesson

    0 of 18 lessons complete in this chapter0%