(Lispex)sicp.io
4.5 · Derived syntax

Rewrite new syntax into a rule the evaluator already knows.

A let expression can be translated into a lambda application, keeping the evaluator core smaller while preserving the binding behavior.

Guiding question

What must remain unchanged when let is rewritten as a lambda application?

  • Separate surface syntax from evaluator core forms
  • Collect binding names as lambda parameters
  • Collect binding expressions as application operands
  • Evaluate the transformed expression in the original environment

The transformer takes the binding names from let and places them in a lambda parameter list. It puts the let body in that lambda and places the binding expressions after the lambda as application operands.

The evaluator does not need a second implementation of local binding. Its let case rewrites the expression and sends the result back through the ordinary lambda and application cases in the same environment.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(begin
  (define (binding-name binding) (car binding))
  (define (binding-value binding) (cadr binding))
  (define (let->combination expression)
    (let ((bindings (cadr expression))
          (body (caddr expression)))
      (cons (list 'lambda (map binding-name bindings) body)
            (map binding-value bindings))))
  (let->combination '(let ((x 10) (y 32)) (+ x y))))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source377 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    The first program returns ((lambda (x y) (+ x y)) 10 32). The second returns 42.

    Trace focus

    In the second run, find the let dispatch and the transformer calls before the lambda is created. The later application extends the environment with x and y, then evaluates the original body.

    Try it yourself

    Change the program before you read the hint.

    Add a z binding with value 8 and change the body to (+ (+ x y) z). Predict both the transformed expression and its value.

    Show one hint

    The parameter and operand lists grow in the same order. The body remains one expression inside the lambda.