See it run
LISPEX
(fold-right cons (list) (list 1 2 3))Observed result
OUTPUT
(1 2 3)How to reason about it
- List, string, and vector traversal is left to right.
fold-rightis the deliberate exception: it calls(f element accumulator)from the right. reduceandfold-leftare the same left fold and call(f accumulator element).all?andany?stop as soon as the answer is known. Their predicate must return#tor#f;filteruses ordinary Lispex truthiness instead.for-eachruns for effect, accepts any number of callback values, and returns zero values itself.- Callback errors and one-shot escapes stay in the guest evaluator. LIL does
not hand the procedure to host
applyor a host callback.
Callback contracts
| Signature | Traversal / result | Value context |
|---|---|---|
| (map proc list) | left-to-right; fresh proper list | callback exactly one value |
| (filter predicate list) | left-to-right; retained elements | predicate exactly one value; only #f rejects |
| (all? predicate list), (any? predicate list) | left-to-right and short-circuiting; strict boolean | predicate exactly one boolean |
| (reduce proc init list), (fold-left proc init list) | left accumulator | callback exactly one value |
| (fold-right proc init list) | right accumulator | callback exactly one value |
| (string-map proc string), (vector-map proc vector) | left-to-right; fresh result | callback exactly one value; string result must be a character |
| string/vector/list for-each | left-to-right effects; overall zero values | callback results are discarded |
| (apply proc arg ... final-list) | shared application dispatcher; tail-safe | procedure result packet preserved |
A common mistake
These procedures accept one collection, not the multi-list variants offered by some Scheme systems. String characters and vector elements are snapshotted before the first callback, so mutating the source vector during traversal does not change which elements are visited.
Keep going
Use this page while writing code. For a guided explanation, return to the syntax map or the corresponding manual.