Higher-Order Procedures

map, filter, reduce, folds, for-each, vector/string variants, and apply call guest procedures through the same application path the program that runs your rule uses.

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, and 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, while 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 inside the program that runs your rule. LIL does not hand the procedure to the host's own apply or to 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, and only #f rejects
(all? predicate list), (any? predicate list)left-to-right and stopping early, 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, and a 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-safeevery value the procedure returns is 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

The guide assembles these procedures into one working pipeline, and the index places them among the other families.

Working with map, filter, fold, and apply · Procedure Index