sicp.io
2.2.3 · Sequence pipelines

Name each stage, then compose the flow.

Enumeration, filtering, mapping, and accumulation separate one data question into stages that can be understood and replaced independently.

Guiding question

How does a sequence pipeline turn nested recursion into named stages?

  • Enumerate a finite input interval
  • Select values with a predicate and transform them with a procedure
  • Accumulate a sequence into one result
  • Read intermediate sequence shapes before predicting the final value

enumerate-interval creates the source sequence. filter keeps only values accepted by odd?, and map replaces every retained value with its square. Each procedure owns one decision.

accumulate combines the transformed sequence with + and the initial value 0. The complete program still recurses, but its control is distributed across reusable stages instead of fused into one special-purpose procedure.

SICP code802 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 program returns 84 from 1² + 3² + 5² + 7².

    Trace focus

    Follow the interval list into filter, then compare the shorter odd list with the squared list consumed by accumulate. The final additions happen only after the stages have established those intermediate shapes.

    Try it yourself

    Change the program and compare the result.

    Change odd? to even?, then use cons and '() instead of + and 0 in accumulate. Predict the resulting list and its order.

    Show hint

    The final stage can receive any two-argument procedure and matching initial value.

    Complete this lesson

    0 of 20 lessons complete in this chapter0%