FoldWhileList[f,x,{a1,a2,…},test]
returns {x,f[x,a1],f[f[x,a1],a2],…}, repeatedly applying f with subsequent values ai until applying test to the result does not yield True.
FoldWhileList[f,list,test]
is equivalent to FoldWhileList[f,First[list],Rest[list],test].
FoldWhileList[f,x,{a1,a2,…},test,m]
supplies the most recent m results as arguments for test at each step.
FoldWhileList[f,x,{a1,a2,…},test,All]
supplies all results so far as arguments for test at each step.
FoldWhileList[f,x,{a1,a2,…},test,m,n]
returns n additional results by applying f an extra n times after test fails.
FoldWhileList[f,x,{a1,a2,…},test,m,-n]
returns n fewer results.
FoldWhileList[f,test]
represents an operator form of FoldWhileList that can be applied to expressions.
FoldWhileList
FoldWhileList[f,x,{a1,a2,…},test]
returns {x,f[x,a1],f[f[x,a1],a2],…}, repeatedly applying f with subsequent values ai until applying test to the result does not yield True.
FoldWhileList[f,list,test]
is equivalent to FoldWhileList[f,First[list],Rest[list],test].
FoldWhileList[f,x,{a1,a2,…},test,m]
supplies the most recent m results as arguments for test at each step.
FoldWhileList[f,x,{a1,a2,…},test,All]
supplies all results so far as arguments for test at each step.
FoldWhileList[f,x,{a1,a2,…},test,m,n]
returns n additional results by applying f an extra n times after test fails.
FoldWhileList[f,x,{a1,a2,…},test,m,-n]
returns n fewer results.
FoldWhileList[f,test]
represents an operator form of FoldWhileList that can be applied to expressions.
Details
- FoldWhileList[f,x,{a1,a2,…},test] returns a list whose first element is x and whose last element is the first expression f[… f[f[x,a1],a2]…,ak] to which applying test does not yield True.
- If test[x] does not yield True, FoldWhileList[f,x,alist,test] gives {x}.
- FoldWhileList[f,x,alist,test] is equivalent to FoldWhileList[f,x,alist,test,1].
- FoldWhileList[f,x,alist,test,m] is equivalent to FoldWhileList[f,x,alist,test,m,0].
- FoldWhileList[f,x,alist,UnsameQ,All] goes on applying f until the same result first appears more than once.
- You can use Throw to exit from FoldWhileList before it is finished.
- FoldWhileList[f,test][list] is equivalent to FoldWhileList[f,list,test].
- FoldWhileList[f,test][x,list] is equivalent to FoldWhileList[f,x,list,test].
Examples
open all close allBasic Examples (3)
Divide 5! by increasing positive integers, until the result is a noninteger:
FoldWhileList[Divide, 5!, Range[10], IntegerQ]Add 1/n8 terms until Unequal yields False for two consecutive results in machine arithmetic:
FoldWhileList[Plus, 0., 1 / Range[100] ^ 8, Unequal, 2]Divide 5! by consecutive positive integers and return the last integer result:
FoldWhileList[Divide, 5!, Range[10], IntegerQ, 1, -1]Scope (8)
Starting with 12, subtract increasing integers while the remainder is still positive:
FoldWhileList[Subtract, 12, {1, 2, 3, 4, 5, 6}, Positive]Compute a random walk starting from the origin, and return the first point whose norm is beyond 5:
FoldWhileList[Plus, {0, 0}, AngleVector /@ RandomReal[2Pi, 50], Norm /* LessThan[5]]%//Last//NormFold Cross over a list of random unit vectors while the norm of the result is larger than 0.1:
FoldWhileList[Cross, Normalize /@ RandomReal[1, {100, 3}], Norm /* GreaterThan[0.1]]%//Last//NormAdd 1/n8 terms until Unequal yields False for two consecutive results in machine arithmetic:
FoldWhileList[Plus, 0., 1 / Range[200] ^ 8, Unequal, 2]Add random integers to 0 while all results are different, returning the first value that was repeated:
SeedRandom[123];FoldWhileList[Plus, 0, DeleteCases[RandomInteger[{-10, 10}, 100], 0], UnsameQ, All]Divide 5! by consecutive positive integers, returning the first noninteger result:
FoldWhileList[Divide, 5!, Range[10], IntegerQ, 1, 0]Return the last integer result:
FoldWhileList[Divide, 5!, Range[10], IntegerQ, 1, -1]Return the next-to-last integer result:
FoldWhileList[Divide, 5!, Range[10], IntegerQ, 1, -2]Abort the computation before it is finished:
Catch[FoldWhileList[If[# > 25, Divide[#1, #2], Throw[$Failed]]&, 5!, Range[10], IntegerQ]]Use FoldWhileList in operator form:
FoldWhileList[Subtract, Positive][12, {1, 2, 3, 4, 5, 6}]FoldWhileList[Plus, LessThan[20]][Range[10]]Properties & Relations (4)
If test[x] does not yield True, FoldWhileList[f,x,alist,test] returns {x}:
FoldWhileList[Divide, 0, {2, 3, 4, 5}, Positive]FoldWhile[f,x,alist,test,…] is equivalent to Last[FoldWhileList[f,x,alist,test,…]]:
FoldWhileList[Divide, 5!, Range[10], IntegerQ]Last[%]FoldWhile[Divide, 5!, Range[10], IntegerQ]FoldWhileList[f,x,list,True&] is equivalent to FoldList[f,x,list]:
FoldWhileList[Plus, 0, Range[10], True&]FoldList[Plus, 0, Range[10]]FoldWhileList[f,x,list,test,m,n] is equivalent to NestWhileList[f,x,test,m,Length[list],n] for a function f taking only one argument:
FoldWhileList[Sqrt[#]&, 10 ^ 6, Range[4], GreaterThan[2], 1, 0]NestWhileList[Sqrt[#]&, 10 ^ 6, GreaterThan[2], 1, 4, 0]Related Guides
History
Text
Wolfram Research (2020), FoldWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/FoldWhileList.html.
CMS
Wolfram Language. 2020. "FoldWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FoldWhileList.html.
APA
Wolfram Language. (2020). FoldWhileList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FoldWhileList.html
BibTeX
@misc{reference.wolfram_2026_foldwhilelist, author="Wolfram Research", title="{FoldWhileList}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/FoldWhileList.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_foldwhilelist, organization={Wolfram Research}, title={FoldWhileList}, year={2020}, url={https://reference.wolfram.com/language/ref/FoldWhileList.html}, note=[Accessed: 12-June-2026]}