(Lispex)sicp.io
2.5 · 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.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(begin
  (define (enumerate-interval low high)
    (if (> low high)
        '()
        (cons low (enumerate-interval (+ low 1) high))))
  (define (filter predicate items)
    (cond ((null? items) '())
          ((predicate (car items))
           (cons (car items) (filter predicate (cdr items))))
          (else (filter predicate (cdr items)))))
  (define (map procedure items)
    (if (null? items)
        '()
        (cons (procedure (car items))
              (map procedure (cdr items)))))
  (define (accumulate operation initial items)
    (if (null? items)
        initial
        (operation (car items)
                   (accumulate operation initial (cdr items)))))
  (define (square value) (* value value))
  (accumulate + 0
    (map square
      (filter odd? (enumerate-interval 1 8)))))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source802 / 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 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 before you read the hint.

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

    Show one hint

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