Anamorphism is a functional-programming and category-theory concept for an “unfold”: a function that builds up a larger data structure (such as a list, tree, or stream) from a single seed value by repeatedly applying a rule until a stopping condition is met. It is the dual, or opposite, of a catamorphism, which “folds” a data structure down into one value.
Put simply, if a catamorphism collapses many values into one, an anamorphism grows one value into many. Anamorphisms are also known as unfold functions, and together with catamorphisms (“folds”) they give functional programming a clean, reusable way to generate and consume recursive data.
Note that “anamorphism” has a second, unrelated meaning in optics and art (anamorphosis), covered further below.
Anamorphism in Functional Programming
In programming, an anamorphism uses a technique called unfold or corecursion. It starts from an initial input and repeatedly applies a function to produce the next element of a structure, stopping when a specified condition is reached. An anamorphism has three core parts:
- Seed value: the initial input the process starts from.
- Transformation function: the rule that produces the next element and the next seed.
- Termination condition: the check that decides whether to keep unfolding or stop.
Because the rule is applied generically, anamorphisms make it easy to write reusable functions that work across many data types — lists, trees, graphs, and even infinite streams built with lazy evaluation.
Concrete examples of anamorphism
- Digits of a number: unfolding an integer into a list of its individual digits.
- Ranges and sequences: generating
[1, 2, 3, …, n]from a starting value and a step rule. - Infinite streams: lazily producing an unbounded sequence (like the natural numbers) that consumers read on demand.
- Tree building: constructing a tree structure from a compact description or seed.
Anamorphisms appear in functional languages such as Haskell, Scala, and Lisp, but they can be implemented anywhere recursion or iteration is available. They pair naturally with reactive patterns — see functional reactive programming for how streams of generated values drive event-based systems.
Anamorphism vs. Catamorphism
The two are mirror images:
- Anamorphism (unfold): one value → a data structure. It builds.
- Catamorphism (fold): a data structure → one value. It reduces.
Used together, unfolds and folds let you generate a structure and then collapse it back into a result — a powerful, composable toolkit for recursive data handling.
Anamorphism in Optics and Art (Anamorphosis)
Outside computer science, “anamorphism” (more precisely anamorphosis) refers to a distorted image that only appears correct when viewed from a specific angle or through a special mirror or lens. Classic uses include:
- Anamorphic lenses in film: they horizontally squeeze a wide image onto the sensor, which is then de-squeezed in post to produce a wide, cinematic aspect ratio.
- Anamorphic art: stretched or skewed artwork that resolves into a recognizable picture from one vantage point.
- Projection mapping: 3D illusions painted or projected onto 2D surfaces that look correct from a chosen viewpoint.
The two meanings share the underlying idea of a systematic transformation — one for data, one for images — which is why the same word is used in both fields.
Anamorphism FAQ
What is an anamorphism in simple terms?
An anamorphism is an “unfold”: a function that grows a data structure (like a list or tree) from a single starting value by applying a rule over and over until a stopping condition is met. It is the opposite of folding a structure down to one value.
What is the difference between anamorphism and catamorphism?
An anamorphism builds a data structure from one value (unfold), while a catamorphism reduces a data structure to a single value (fold). They are duals of each other and are often used together.
What programming languages support anamorphisms?
Anamorphisms are common in functional languages such as Haskell, Scala, and Lisp, but any language that supports recursion or iteration can implement an unfold-style anamorphic function.
Why is anamorphism also used in film and photography?
In optics, anamorphism (anamorphosis) describes an image that is intentionally distorted and then corrected — for example, an anamorphic lens squeezes a wide scene and it is de-squeezed later. It shares the “systematic transformation” idea with the programming concept.
Related Technology Terms
- Catamorphism (Fold)
- Corecursion
- Recursive Data Structures
- Lazy Evaluation
- Aspect Ratio Conversion