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 left-branching binary tree:
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:
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