Fold
Examples
open allclose allBasic Examples (4)
Scope (8)
Use a pure function with Fold :
Use the operator form of Fold on one argument:
Use the operator form of Fold on two arguments:
Created nested powers with left-associativity:
Created nested powers with right-associativity:
Perform two subsequent permutations:
Perform a chain of cross products:
The head of the third argument need not be List :
Applications (9)
Create a nested polynomial (Horner form):
HornerForm directly produces this output:
Form a continued fraction:
Form a number from digits:
Form an alternating sum:
Form a binary tree:
Form a left-branching binary tree:
Form a function composition:
Apply an indexed sequence of functions:
Successively partition a list:
Properties & Relations (9)
Folding with an empty list does not apply the function at all:
Equivalently, this does not apply the function at all:
Fold returns the last element of FoldList :
Fold requires a function f that takes the seed in its first argument:
To use the seed in the second argument, use ReverseApplied :
Fold takes list elements from left to right:
Use Reverse to take elements from right to left:
Combine Reverse and ReverseApplied :
Folding an additional element is equivalent to applying the function to the additional element:
Fold [f,x,{a,b,…}] is equivalent to Fold [f,f[x,a],{b,…}]:
Functions that ignore their second argument give the same result as in Nest :
FoldList can be computed with NestWhileList :
Possible Issues (1)
An empty list cannot be folded without a seed:
However, the action of FoldList is well defined:
See Also
Nest FoldList FoldPair SequenceFold ParallelCombine
Function Repository: FoldRight FoldIndexed FoldThread
Tech Notes
Related Guides
History
Introduced in 1991 (2.0) | Updated in 1996 (3.0) ▪ 2014 (10.0) ▪ 2016 (11.0)
Text
Wolfram Research (1991), Fold, Wolfram Language function, https://reference.wolfram.com/language/ref/Fold.html (updated 2016).
CMS
Wolfram Language. 1991. "Fold." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/Fold.html.
APA
Wolfram Language. (1991). Fold. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Fold.html
BibTeX
@misc{reference.wolfram_2025_fold, author="Wolfram Research", title="{Fold}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Fold.html}", note=[Accessed: 11-April-2025 ]}
BibLaTeX
@online{reference.wolfram_2025_fold, organization={Wolfram Research}, title={Fold}, year={2016}, url={https://reference.wolfram.com/language/ref/Fold.html}, note=[Accessed: 11-April-2025 ]}