Skip to content

Programming Fold

A parameterized reduction of structured data using a supplied base and combining operation, with right-recursive or left-accumulator variants.

Version
v1 · 2026-10-03 · History
Domain-specific #
13525
Aliases
Fold Higher Order Function, List Fold, Catamorphic Fold

Core Idea

A programming fold reduces structured input using a supplied base and combining operation. In the right-fold/catamorphic variant, handlers combine already folded children: foldr f z [] = z and foldr f z (x:xs) = f x (foldr f z xs). Direct foldl instead threads an updated accumulator through the remaining list: foldl f z (x:xs) = foldl f (f z x) xs. These are different recursive equations within a fold family, not merely two performance settings. The selected operation can produce a sum, another list, or something else; a fold need not compress data to a scalar.[ref-5c133d8b4ab2][ref-d889ef218b7f]

Scope of Application

Haskell's foldr and foldl associate list operations from opposite directions; foldl' is the strict left-fold variant. These choices matter for nonassociative operations, lazy productivity and space behavior. A right fold can sometimes return a finite result from an infinite lazy input only when its handler does not demand the entire tail; the ordinary left fold in the Haskell Report requires a finite list.[^ref-5c133d8b4ab2]

The constructor-handler idea also applies to finite expression trees and other inductive datatypes. An evaluator may map Lit n to n and Add to addition of its folded child results. This is the same catamorphic pattern, with tree rather than list constructors.[^ref-d889ef218b7f]

Clarity

The fold separates the shape of the input, the algebra supplied for its constructors, and the evaluation order. foldr (+) 0 [2,3,4] gives 9, but foldr (:) [] reconstructs a list. The live prime named Fold instead denotes physical bending under stress; it is a lexical homonym, not this programming identity.

Manages Complexity

Many list and tree operations share a constructor-following traversal. A fold packages that recursion scheme once, so new functions can be described by the handlers they supply. This supports algebraic program reasoning while leaving the handler's actual strictness and cost open to analysis.[^ref-d889ef218b7f]

Abstract Reasoning

Identify the structured input and base value, then choose a recurrence. For a right fold or tree catamorphism, ask how each constructor combines already-folded children; an operation that also needs original subtrees may require a richer scheme. For direct foldl, ask how each next element updates the carried accumulator before the tail is traversed. Expand the chosen formula and check finiteness, strictness and whether a right-fold handler demands the tail.[ref-5c133d8b4ab2][ref-d889ef218b7f]

Knowledge Transfer

The right-fold/catamorphism scheme transfers literally from lists to other inductive datatypes when their constructors and handler algebra are explicit; direct left-fold accumulator threading remains a distinct list variant. The broader rule-as-value pattern is represented by the live Higher Order Function prime, proposed as this entry's structural prerequisite. The mechanical Fold prime is unrelated; ordinary aggregation or unfolding from a seed is not automatically a programming fold.[^ref-d889ef218b7f]

[^ref-5c133d8b4ab2]: Simon Marlow, ed., Haskell 2010 Language Report, original language specification, Data.List §20.3, PDF p.210. [^ref-d889ef218b7f]: Erik Meijer, Maarten Fokkinga and Ross Paterson, “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire”, original 1991 paper in the CWI repository, §§2–3.

Relationships to Other Abstractions

Local relationship map for Programming FoldParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Programming FoldDOMAINPrime abstraction: Higher Order Function — presupposesHigher OrderFunctionPRIME

Current abstraction Programming Fold Domain-specific

Parents (1) — more general patterns this builds on

  • Programming Fold presupposes Higher Order Function Prime

    A generic fold takes constructor-handling operations as parameters, structurally using a function as a value.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Programming Fold sits in a moderately populated region (54th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.

Family — Recursive Construction Schemes (8 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-10-08