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.[1][2]

How would you explain it like I'm…

Squish It All Together

Imagine a row of blocks and a rule for squishing two things into one. You start with a starting thing, then use the rule to squish in one block at a time until the whole row becomes one thing. That's a fold. It might make one number, like a total, or even something new like another row.

Boiling a List Down

A fold is a way to boil down a list, or another structure, into one result. You choose two things: a starting value and a rule for combining. For example, with a start of 0 and the rule 'add', folding the list 1, 2, 3 gives 6. But the result doesn't have to be a number; with other rules you can build a new list or something else. Folds can go from the right end or from the left end, and the two ways don't always work the same.

Higher-Order Structured Reduction

A fold is a reusable way to reduce a structured input, like a list or a tree, to one value, using a base value and a combining function you supply. Because you pass the combining rule in as a function, fold is a higher-order function. A right fold combines each element with the already-folded rest of the list: foldr f z (x:xs) = f x (foldr f z xs). A left fold instead updates an accumulator first and then moves on down the list: foldl f z (x:xs) = foldl f (f z x) xs. The result can be any type, not just a number: foldr (+) 0 sums a list, but foldr (:) [] rebuilds the list. In a lazy language, a right fold can sometimes give an answer from an infinite list if the combining function does not need the whole rest, but no fold is guaranteed to finish on arbitrary infinite input.

 

A programming fold is a parameterized family of structured reductions, defined by a base value and a supplied combining operation, and therefore higher-order. The variants share this parameterization but not one recursive equation. The direct right list fold substitutes each recursive child by its folded result before applying the handler (foldr f z (x:xs) = f x (foldr f z xs), with foldr f z [] = z), while the direct left fold updates an accumulator before recursing on the tail (foldl f z (x:xs) = foldl f (f z x) xs). Meijer, Fokkinga and Paterson characterize the right list fold as a catamorphism and generalize constructor-based recursion schemes to algebraic datatypes, while the Haskell Report specifies both list directions. The result is a single value of an arbitrary type: foldr (+) 0 sums a finite list, foldr (:) [] reconstructs it, and tree catamorphisms can yield trees or tuples. Variants must not be conflated: foldl requires a finite list per the Report, foldl' is its strict variant, and lazy right folds can produce a finite answer or output prefix from infinite input only when the handler does not demand the full tail; no variant guarantees termination on arbitrary infinite data.

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.[1]

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.[2]

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.[2]

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.[1][2]

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.[2]

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) — outgoing typed relations

  • 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 (50th 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-11-14

References

[1] Simon Marlow, ed., Haskell 2010 Language Report, original language specification, Data.List §20.3, PDF p.210. registry ↩a ↩b ↩c

[2] 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. registry ↩a ↩b ↩c ↩d ↩e