GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — FoldWhile—Wolfram Documentation

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,list,test]

is equivalent to FoldWhile[f,First[list],Rest[list],test].

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,All]

supplies all results so far 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[f,test]

represents an operator form of FoldWhile that can be applied to expressions.

Details
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Properties & Relations  
See Also
Related Guides
History
Cite this Page

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,list,test]

is equivalent to FoldWhile[f,First[list],Rest[list],test].

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,All]

supplies all results so far 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[f,test]

represents an operator form of FoldWhile that can be applied to expressions.

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 all

Basic Examples  (3)

Divide 5! by consecutive positive integers, until the result is a non-integer:

Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ]

Add 1/n6 terms until Unequal yields False for two consecutive results in machine arithmetic:

Wolfram Language code: FoldWhile[Plus, 0., 1 / Range[1000] ^ 6, Unequal, 2]

Divide 5! by consecutive positive integers and return the last integer result:

Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -1]

Scope  (9)

Starting with 12, subtract increasing integers while the remainder is still positive:

Wolfram Language code: FoldWhile[Subtract, 12, {1, 2, 3, 4, 5, 6}, Positive]

Use Echo to display the intermediate results:

Wolfram Language code: FoldWhile[Echo @* Subtract, 12, {1, 2, 3, 4, 5, 6}, Positive]

Display the results of the corresponding tests:

Wolfram Language code: 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:

Wolfram Language code: FoldWhile[Plus, {0, 0}, AngleVector /@ RandomReal[2Pi, 200], Norm /* LessThan[10]]
Wolfram Language code: Norm[%]

Fold Cross over a list of random unit vectors while the norm of the result is larger than 0.1:

Wolfram Language code: FoldWhile[Cross, Normalize /@ RandomReal[1, {100, 3}], Norm /* GreaterThan[0.1]]
Wolfram Language code: Norm[%]

Add 1/n8 terms until Unequal yields False for two consecutive results in machine arithmetic:

Wolfram Language code: 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:

Wolfram Language code: SeedRandom[123];
Wolfram Language code: FoldWhile[Plus, 0, DeleteCases[RandomInteger[{-10, 10}, 100], 0], UnsameQ, All]

Divide 5! by consecutive positive integers, returning the first non-integer result:

Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, 0]

Return the last integer result:

Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -1]

Return the next-to-last integer result:

Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ, 1, -2]

Abort the computation before it is finished:

Wolfram Language code: Catch[FoldWhile[If[# > 25, Divide[#1, #2], Throw[#1]]&, 5!, Range[10], IntegerQ]]

Use FoldWhile in operator form:

Wolfram Language code: FoldWhile[Subtract, Positive][12, {1, 2, 3, 4, 5, 6}]
Wolfram Language code: FoldWhile[Plus, LessThan[20]][Range[10]]

FoldWhile admits operator forms of three or four arguments when the third and fourth arguments are explicit integers:

Wolfram Language code: 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:

Wolfram Language code: FoldWhile[Divide, 0, {2, 3, 4, 5}, Positive]

FoldWhile[f,x,alist,test,…] is equivalent to Last[FoldWhileList[f,x,alist,test,…]]:

Wolfram Language code: FoldWhileList[Divide, 5!, Range[10], IntegerQ]
Wolfram Language code: Last[%]
Wolfram Language code: FoldWhile[Divide, 5!, Range[10], IntegerQ]

FoldWhile[f,x,list,True&] is equivalent to Fold[f,x,list]:

Wolfram Language code: FoldWhile[Plus, 0, Range[10], True&]
Wolfram Language code: 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:

Wolfram Language code: FoldWhile[Sqrt[#]&, 10 ^ 6, Range[4], GreaterThan[2], 1, 0]
Wolfram Language code: NestWhile[Sqrt[#]&, 10 ^ 6, GreaterThan[2], 1, 4, 0]

See Also

FoldWhileList  Fold  NestWhile  FoldPair  SequenceFold

Related Guides

    ▪
  • Functional Iteration
  • ▪
  • Functional Programming

History

Introduced in 2020 (12.2)

Wolfram Research (2020), FoldWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/FoldWhile.html.

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: 12-November-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: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.