FoldList
Details
- FoldList[Plus,0,list] generates cumulative sums of the elements in list.
- With a length n list, FoldList generates a list of length
. » - The head of list in FoldList[f,x,list] need not be List.
- FoldList[f][list] is equivalent to FoldList[f,list].
- FoldList[f][x,list] is equivalent to FoldList[f,x,list].
Examples
open all close allBasic Examples (6)
Successively apply a function f to a seed x and the elements of a list:
FoldList[f, x, {a, b, c, d}]Cumulative sums of the elements of the list:
FoldList[Plus, 0, {a, b, c, d}]FoldList[#1 ^ #2&, x, {a, b, c, d}]Start from the first element of the list:
FoldList[f, {a, b, c, d}]Use the operator form of FoldList on one argument:
FoldList[f][{a, b, c, d}]Use the operator form of FoldList on two arguments:
FoldList[f][x, {a, b, c, d}]Scope (2)
Generalizations & Extensions (2)
The head need not be List:
FoldList[f, x, p[a, b, c, d]]FoldList[g[#2, #1]&, x, {a, b, c, d}]Applications (15)
Compute successive factorials:
FoldList[Times, 1, Range[10]]Compute products of successive primes:
FoldList[Times, 1, Array[Prime, 10]]FoldList[Plus, 0, {a, b, c, d}]Build up a continued fraction:
FoldList[1 / (#2 + #1)&, x, Reverse[{a, b, c}]]Build up a nested polynomial (Horner form):
FoldList[x #1 + #2&, 0, {a, b, c, d}]Build up a number from digits:
FoldList[10#1 + #2&, 0, {4, 5, 1, 6, 7, 8}]FoldList[#2 - #1&, 0, {a, b, c, d, e}]FoldList[Plus, 0, Table[(-1) ^ RandomInteger[], {20}]]Find the running maximum of a list:
FoldList[Max, 0, {1, 3, 1, 2, 4, 1, 5, 3, 6, 2, 8, 11}]DeleteDuplicates[%]Find successively deeper parts in an expression:
FoldList[#1[[#2]]&, (a + b)(x ^ 2 + y ^ 2), {2, 1, 1}]FoldList[Part, (a + b)(x ^ 2 + y ^ 2), {2, 1, 1}]Successively append to a list:
FoldList[Append, {x}, {a, b, c, d}]FoldList[List, x, {a, b, c, d}]Build up a left-branching binary tree:
FoldList[{#2, #1}&, x, {a, b, c, d}]FoldList[#2[#1]&, x, {a, b, c, d}]Compute a power using a power tree with successive squaring:
With[{m = x, t = 85}, FoldList[#1 ^ 2 m ^ #2&, 1, IntegerDigits[t, 2]]]With[{m = 3, t = 85}, FoldList[#1 ^ 2 m ^ #2&, 1, IntegerDigits[t, 2]]]Properties & Relations (5)
FoldList makes a list of length
:
Length[FoldList[f, x, Range[10]]]Rest gives a result of length
:
Rest[FoldList[Plus, 0, {a, b, c, d, e}]]Folding with an empty list does not apply the function at all:
FoldList[f, x, {}]Fold gives the last element of FoldList:
FoldList[f, x, {a, b, c}]Fold[f, x, {a, b, c}]Functions that ignore their second argument give the same result as in NestList:
FoldList[f[#1]&, x, Range[5]]NestList[f, x, 5]Accumulate is equivalent to FoldList with Plus:
FoldList[Plus, {a, b, c, d}]Accumulate[{a, b, c, d}]Possible Issues (1)
See Also
Fold FoldPairList SequenceFoldList Accumulate Differences Ratios NestList ComposeList ExponentialMovingAverage Partition MapIndexed AnglePath
Function Repository: FoldIndexedList FoldRightList FoldThreadList
Tech Notes
Related Guides
History
Introduced in 1991 (2.0) | Updated in 2012 (9.0) ▪ 2016 (11.0)
Text
Wolfram Research (1991), FoldList, Wolfram Language function, https://reference.wolfram.com/language/ref/FoldList.html (updated 2016).
CMS
Wolfram Language. 1991. "FoldList." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/FoldList.html.
APA
Wolfram Language. (1991). FoldList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FoldList.html
BibTeX
@misc{reference.wolfram_2026_foldlist, author="Wolfram Research", title="{FoldList}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/FoldList.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_foldlist, organization={Wolfram Research}, title={FoldList}, year={2016}, url={https://reference.wolfram.com/language/ref/FoldList.html}, note=[Accessed: 13-June-2026]}