See it run
LISPEX
(reduce + 0 (map (lambda (x) (* x x)) (filter odd? (list 1 2 3 4))))Observed result
OUTPUT
10Walk through the pattern
- Select.
filterkeeps1and3becauseodd?is true for them. - Transform.
mapsquares the retained values, producing(1 9)in the same order. - Combine.
reducestarts at0and adds each square, producing10.
How to reason about it
- Use
mapfor one result per element,filterfor truth-valued selection, and folds for one accumulated result. - Use
all?orany?when a strict boolean answer can stop the traversal early. - Callbacks execute as guest procedures, so errors, escapes, warnings, and value-context rules remain active.
string-mapandvector-mapproduce fresh collections; theirfor-eachvariants run only for effects and return zero values.- Use
applywhen a final proper list should become positional arguments; it preserves tail application.
Check yourself
Replace odd? with even?. What does the same pipeline return?
Answer
It keeps 2 and 4, squares them to (4 16), and returns 20.
A common mistake
A callback that yields zero or multiple values in a single-value traversal position raises E320.
Keep going
Use the reference for exact callback arities. Use recursion when traversal needs a shape that no maintained combinator expresses clearly.