FoldWhile[f,x,{a1,a2,…},test]
returns the first expression f[… f[f[x,a1],a2]…,ak] to which applying test does not yield True.
FoldWhile[f,x,{a1,a2,…},test,m]
supplies the most recent m results as arguments for test at each step.
FoldWhile[f,x,{a1,a2,…},test,m,n]
returns the result after applying f an extra n times after test fails.
FoldWhile[f,x,{a1,a2,…},test,m,-n]
returns the result found when f had been applied n fewer times.
FoldWhile
FoldWhile[f,x,{a1,a2,…},test]
returns the first expression f[… f[f[x,a1],a2]…,ak] to which applying test does not yield True.
FoldWhile[f,x,{a1,a2,…},test,m]
supplies the most recent m results as arguments for test at each step.
FoldWhile[f,x,{a1,a2,…},test,m,n]
returns the result after applying f an extra n times after test fails.
FoldWhile[f,x,{a1,a2,…},test,m,-n]
returns the result found when f had been applied n fewer times.
Details
- FoldWhile[f,x,{a1,a2,…},test] returns the last element of FoldWhileList[f,x,{a1,a2,…},test].
- If test[x] does not yield True, FoldWhile[f,x,alist,test] returns x.
- FoldWhile[f,x,alist,test] is equivalent to FoldWhile[f,x,alist,test,1].
- FoldWhile[f,x,alist,test,m] is equivalent to FoldWhile[f,x,alist,test,m,0].
- FoldWhile[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 FoldWhile before it is finished.
- FoldWhile[f,test][list] is equivalent to FoldWhile[f,list,test].
- FoldWhile[f,test][x,list] is equivalent to FoldWhile[f,x,list,test].
Examples
open all close allBasic Examples (3)
Divide 5! by consecutive positive integers, until the result is a non-integer:
FoldWhile[Divide, 5!, Range[10], IntegerQ]Add 1/n6 terms until Unequal yields False for two consecutive results in machine arithmetic:
FoldWhile[Plus, 0., 1 / Range[1000] ^ 6, Unequal, 2]Divide 5! by consecutive positive integers and return the last integer result:
FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -1]Scope (9)
Starting with 12, subtract increasing integers while the remainder is still positive:
FoldWhile[Subtract, 12, {1, 2, 3, 4, 5, 6}, Positive]Use Echo to display the intermediate results:
FoldWhile[Echo @* Subtract, 12, {1, 2, 3, 4, 5, 6}, Positive]Display the results of the corresponding tests:
FoldWhile[Subtract, 12, {1, 2, 3, 4, 5, 6}, Echo @* Positive]Compute a random walk starting from the origin, and return the first point whose norm is beyond 10:
FoldWhile[Plus, {0, 0}, AngleVector /@ RandomReal[2Pi, 200], Norm /* LessThan[10]]Norm[%]Fold Cross over a list of random unit vectors while the norm of the result is larger than 0.1:
FoldWhile[Cross, Normalize /@ RandomReal[1, {100, 3}], Norm /* GreaterThan[0.1]]Norm[%]Add 1/n8 terms until Unequal yields False for two consecutive results in machine arithmetic:
FoldWhile[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];FoldWhile[Plus, 0, DeleteCases[RandomInteger[{-10, 10}, 100], 0], UnsameQ, All]Divide 5! by consecutive positive integers, returning the first non-integer result:
FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, 0]Return the last integer result:
FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -1]Return the next-to-last integer result:
FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -2]Abort the computation before it is finished:
Catch[FoldWhile[If[# > 25, Divide[#1, #2], Throw[#1]]&, 5!, Range[10], IntegerQ]]Use FoldWhile in operator form:
FoldWhile[Subtract, Positive][12, {1, 2, 3, 4, 5, 6}]FoldWhile[Plus, LessThan[20]][Range[10]]FoldWhile admits operator forms of three or four arguments when the third and fourth arguments are explicit integers:
FoldWhile[Subtract, Positive, 1, -1][12, {1, 2, 3, 4, 5, 6}]Properties & Relations (4)
If test[x] does not yield True, FoldWhile[f,x,alist,test] returns x:
FoldWhile[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]FoldWhile[f,x,list,True&] is equivalent to Fold[f,x,list]:
FoldWhile[Plus, 0, Range[10], True&]Fold[Plus, 0, Range[10]]FoldWhile[f,x,list,test,m,n] is equivalent to NestWhile[f,x,test,m,Length[list],n] for a function f taking only one argument:
FoldWhile[Sqrt[#]&, 10 ^ 6, Range[4], GreaterThan[2], 1, 0]NestWhile[Sqrt[#]&, 10 ^ 6, GreaterThan[2], 1, 4, 0]See Also
Related Guides
History
Text
Wolfram Research (2020), FoldWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/FoldWhile.html.
CMS
Wolfram Language. 2020. "FoldWhile." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FoldWhile.html.
APA
Wolfram Language. (2020). FoldWhile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FoldWhile.html
BibTeX
@misc{reference.wolfram_2026_foldwhile, author="Wolfram Research", title="{FoldWhile}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/FoldWhile.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_foldwhile, organization={Wolfram Research}, title={FoldWhile}, year={2020}, url={https://reference.wolfram.com/language/ref/FoldWhile.html}, note=[Accessed: 13-June-2026]}