Higher-Order Procedures

Look up traversal order, callback arguments, return values, and errors for map, filter, folds, for-each, and apply.

Use this reference to check how a traversal calls your procedure and how many values it expects back. You can also check how apply passes arguments from a list.

Build a list from the right

Run this complete example in the Playground.

LISPEX
(fold-right cons (list) (list 1 2 3))

Result

OUTPUT
(1 2 3)

fold-right starts with the empty list and visits 3, 2, then 1. It calls cons with the current element first and the accumulated list second. Each call adds the element to the front, so the final list is (1 2 3).

A callback is the procedure you pass for a traversal to call. An accumulator is the result carried from one step to the next.

Check calls and return values

Each traversal takes one collection. List arguments must be proper lists, whose final tail is the empty list.

In the table, proc and predicate name the procedure you pass. A predicate tests whether an element meets a condition. Traversal is left to right except for fold-right.

CallCallback callResult requirements and overall result
(map proc list)(proc element)exactly one value per call; collects them in a fresh list
(filter predicate list)(predicate element)exactly one value per call; keeps the element unless that value is #f
(all? predicate list), (any? predicate list)(predicate element)exactly one #t or #f per call; returns a boolean as soon as the answer is known
(reduce proc init list), (fold-left proc init list)(proc accumulator element)exactly one value per call becomes the next accumulator; returns the final accumulator
(fold-right proc init list)(proc element accumulator)visits from the right; exactly one value per call becomes the next accumulator; returns the final accumulator
(string-map proc string)(proc character)exactly one character per call; returns a fresh string
(vector-map proc vector)(proc element)exactly one value per call; returns a fresh vector
(for-each proc list), (string-for-each proc string), (vector-for-each proc vector)(proc element)discards any number of callback values; returns zero values

reduce and fold-left are the same procedure. Folds start with init as the accumulator and return it unchanged for an empty list.

Strings and vectors are copied into a sequence of elements before the first callback. Changing the source vector during traversal therefore does not change which elements are visited.

The interpreter calls each supplied procedure itself. Errors, warnings, and one-shot continuation exits follow the ordinary Lispex call rules. See continuations for these exits.

Call once with apply

(apply proc arg ... final-list) calls proc once. It passes any explicit arg values followed by the elements of final-list. The final list must be proper.

apply preserves all values returned by proc. In tail position, it calls proc without keeping an extra return frame. See tail calls.

Locate an error

ErrorWhat to check
E302The number of arguments to the outer procedure and to its callback. Compare both calls with the table. For apply, count the explicit arguments plus the final list's elements.
E320A callback returned zero or multiple values where the table requires one. Return one value, or use a for-each variant when you only need an action such as printing.
E312Check the collection type and proper-list requirement. For all? and any?, return a boolean. For string-map, return a character.

The guide combines selection, transformation, and accumulation. The procedure index lists the other built-ins.