sicp.io
4.1.1 · The core of the evaluator

Eval classifies expressions; apply completes applications.

Study one assembled evaluator core in which syntax predicates, environment operations, compound-procedure records, eval, and apply cooperate.

Guiding question

Which responsibilities belong to eval, which belong to apply, and which data structures connect the two?

  • Classify self-evaluating values, variables, special forms, and applications
  • Route assignment, definition, if, lambda, begin, and quote to distinct rules
  • Evaluate an operator and its operands before applicative-order application
  • Apply either a host primitive or a represented compound procedure
  • Extend a captured lexical environment for every compound application
  • Keep evaluator control separate from the guest program represented as data

evaluate is the central syntax dispatcher. Direct values return themselves, names use environment lookup, special forms receive their own evaluation rules, and every remaining pair is treated as an application. The application case evaluates the operator and operand expressions, then transfers the resulting procedure and argument values to apply-procedure.

apply-procedure receives already classified procedure values. A host procedure receives the argument list through apply. A compound-procedure record supplies parameters, body expressions, and its creation environment; application extends that environment with a fresh frame and evaluates the body sequence. The reused canonical driver exercises every core form in one persistent guest environment and makes the educational eval/apply loop visible.

SICP code7,837 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 complete run returns (complete ((defined square) (defined make-adder) (defined add-five) (defined base) 49 12 (assigned base) 81 6) 0). The five-form-budget run returns (truncated ((defined square) (defined make-adder) (defined add-five) (defined base) 49) 4).

    Trace focus

    For each guest form, begin at evaluate and identify the selected clause. On applications, separate operator evaluation, left-to-right list-of-values construction, and apply-procedure. Follow each compound procedure into a new frame linked to the environment stored in its procedure record. The top-level form budget belongs to the driver and is not part of eval or apply.

    Try it yourself

    Change the program and compare the result.

    Add a guest begin expression whose first form mutates base and whose last form calls add-five. Before running, identify every evaluate clause and the one apply-procedure branch the new form will use.

    Show hint

    begin delegates to eval-sequence. set! mutates an existing frame, while add-five remains a compound procedure carrying the environment created by make-adder.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%