Higher-Order Procedures

`map`, `filter`, `reduce`, folds, `for-each`, vector/string variants, and `apply` invoke guest procedures through the evaluator application path.

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-right is the deliberate exception: it calls (f element accumulator) from the right.
  • reduce and fold-left are the same left fold and call (f accumulator element).
  • all? and any? stop as soon as the answer is known. Their predicate must return #t or #f; filter uses ordinary Lispex truthiness instead.
  • for-each runs 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 apply or a host callback.

Callback contracts

SignatureTraversal / resultValue context
(map proc list)left-to-right; fresh proper listcallback exactly one value
(filter predicate list)left-to-right; retained elementspredicate exactly one value; only #f rejects
(all? predicate list), (any? predicate list)left-to-right and short-circuiting; strict booleanpredicate exactly one boolean
(reduce proc init list), (fold-left proc init list)left accumulatorcallback exactly one value
(fold-right proc init list)right accumulatorcallback exactly one value
(string-map proc string), (vector-map proc vector)left-to-right; fresh resultcallback exactly one value; string result must be a character
string/vector/list for-eachleft-to-right effects; overall zero valuescallback results are discarded
(apply proc arg ... final-list)shared application dispatcher; tail-safeprocedure 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.

Syntax at a Glance · Learning Path