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 allclose allBasic Examples (3)
Scope (8)
Starting with 12, subtract increasing integers while the remainder is still positive:
Compute a random walk starting from the origin, and return the first point whose norm is beyond 5:
Fold Cross over a list of random unit vectors while the norm of the result is larger than 0.1:
Add 1/n8 terms until Unequal yields False for two consecutive results in machine arithmetic:
Add random integers to 0 while all results are different, returning the first value that was repeated:
Divide 5! by consecutive positive integers, returning the first noninteger result:
Return the last integer result:
Return the next-to-last integer result:
Abort the computation before it is finished:
Use FoldWhileList in operator form:
Properties & Relations (4)
If test[x] does not yield True, FoldWhileList[f,x,alist,test] returns {x}:
FoldWhile[f,x,alist,test,…] is equivalent to Last[FoldWhileList[f,x,alist,test,…]]:
FoldWhileList[f,x,list,True&] is equivalent to FoldList[f,x,list]:
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:
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