Fold
Examples
open all close allBasic Examples (4)
Successively apply a function f to a seed x and the elements of a list:
Fold[f, x, {a, b, c, d}]Use Fold with List to create nested ordered pairs:
Fold[List, x, {a, b, c, d}]Multiply elements one element at a time:
Fold[Times, 1, {a, b, c, d}]Start from the first element of the list:
Fold[f, {a, b, c, d}]Scope (8)
Use a pure function with Fold:
Fold[{2 #1, 3#2}&, x, {a, b, c, d}]Use the operator form of Fold on one argument:
Fold[f][{a, b, c, d}]Use the operator form of Fold on two arguments:
Fold[f][x, {a, b, c, d}]Created nested powers with left-associativity:
Fold[Power, {a, b, c, d}]Created nested powers with right-associativity:
Fold[ReverseApplied[Power], Reverse@{a, b, c, d}]Perform two subsequent permutations:
Fold[Permute, {a, b, c, d}, {{1, 4, 3, 2}, {3, 2, 4, 1}}]Perform a chain of cross products:
Fold[Cross, {{1, -1, 1}, {0, 1, 1}, {1, 1, -1}}]The head of the third argument need not be List:
Fold[f, x, p[a, b, c, d]]Use Throw to exit a Fold early:
FoldList[# ^ 2 + #2&, 2, Range[6]]Catch[Fold[If[# > 10 ^ 6, Throw[#], # ^ 2 + #2]&, 2, Range[6]]]Applications (9)
Create a nested polynomial (Horner form):
Fold[x #1 + #2&, 0, {a, b, c, d, e}]HornerForm directly produces this output:
HornerForm[Reverse[{a, b, c, d, e}].x ^ Range[0, 4], x]Fold[1 / (#2 + #1)&, x, Reverse[{a, b, c, d}]]Fold[10#1 + #2&, 0, {4, 5, 1, 6, 7, 8}]Fold[#2 - #1&, 0, Reverse[{a, b, c, d, e}]]Fold[List, x, {a, b, c, d}]Form a left-branching binary tree:
Fold[{#2, #1}&, x, {a, b, c, d}]Fold[#2[#1]&, x, {a, b, c, d}]Apply an indexed sequence of functions:
Fold[{f[#, #], g[#]}[[#2]]&, e, {1, 1, 2, 1, 2}]Successively partition a list:
Fold[Partition, Range[30], {2, 4, 3}]Dimensions[%]Properties & Relations (9)
Folding with an empty list does not apply the function at all:
Fold[f, x, {}]Equivalently, this does not apply the function at all:
Fold[f, {a}]Fold returns the last element of FoldList:
Fold[f, x, {a, b, c}]FoldList[f, x, {a, b, c}]Last[%]Fold requires a function f that takes the seed in its first argument:
Fold[g, x, {a, b, c, d}]To use the seed in the second argument, use ReverseApplied:
Fold[ReverseApplied[g], x, {a, b, c, d}]Fold takes list elements from left to right:
Fold[g, x, {a, b, c, d}]Use Reverse to take elements from right to left:
Fold[g, x, Reverse@{a, b, c, d}]Combine Reverse and ReverseApplied:
Fold[ReverseApplied[g], x, Reverse@{a, b, c, d}]Folding an additional element is equivalent to applying the function to the additional element:
Fold[f, x, {a, b, c}]f[Fold[f, x, {a, b}], c]Fold[f,x,{a,b,…}] is equivalent to Fold[f,f[x,a],{b,…}]:
Fold[f, x, {a, b, c, d}]Fold[f, f[x, a], {b, c, d}]Functions that ignore their second argument give the same result as in Nest:
Fold[f[#1]&, x, Range[5]]Nest[f[#1]&, x, 5]FoldList can be computed with NestWhileList:
FoldList[f, x, {a, b, c}]foldStep[f_][{x_, {a_, b___}}] := {f[x, a], {b}}
First /@ NestWhileList[foldStep[f], {x, {a, b, c}}, MatchQ[{_, {__}}]]Fold[Append,{},list] is equivalent to list:
Fold[Append, {}, {1, 2, 3, 4, 5}]Possible Issues (1)
An empty list cannot be folded without a seed:
Fold[f, {}]However, the action of FoldList is well defined:
FoldList[f, {}]Neat Examples (3)
An explicit form of the primitive recursive function r[z,r[s,r[s,r[s,p[2]]]]] [more info]:
Array[Fold[Fold[2 ^ Ceiling[Log[2, Ceiling[(#1 + 2) / (#2 + 2)]]] (#2 + 2) - 2 - #1&, #2, Range[#1]]&, 0, Range[#]]&, 100]Generate all subsets of a set:
Fold[Function[{s, e}, Join[s, Append[#, e]& /@ s]], {{}}, {a, b, c}]Find all possible sums of any of the elements of a list of numbers:
Fold[Union[#1, #1 + #2]&, {0}, {1, 2, 2, 8}]The fourth Swinnerton–Dyer polynomial [more info]:
Fold[Expand[(#1 /. x -> x + #2)(#1 /. x -> x - #2)]&, x, Sqrt[Prime[Range[4]]]]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_2026_fold, author="Wolfram Research", title="{Fold}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Fold.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_fold, organization={Wolfram Research}, title={Fold}, year={2016}, url={https://reference.wolfram.com/language/ref/Fold.html}, note=[Accessed: 12-June-2026]}