Skip to content

NestWhileList

NestWhileList[f, expr, test, m, max, n] applies f to expr until test does not return True. It returns a list of all intermediate results. test is a function that takes as its argument the last m results. max denotes the maximum number of applications of f and n denotes that f should be applied another n times after test has terminated the recursion.

Attributes[NestWhileList] := {Protected}

Simple examples

In[1]:= Length[NestWhileList[Function[(Slot[1] + (3)/(Slot[1]))/(2)], 1., Function[Slot[1] =!= Slot[2]], 2]]
Out[1]= 7
In[2]:= NestWhileList[Function[Slot[1]^2], 2, Function[(Slot[1]) < (256)]]
Out[2]= {2, 4, 16, 256}
In[3]:= NestWhileList[Function[Slot[1] + 1], 1, Function[((Slot[1] + Slot[4])) < (10)], 4]
Out[3]= {1, 2, 3, 4, 5, 6, 7}
In[4]:= NestWhileList[Function[Slot[1] + 1], 1, Function[True], 1, 4]
Out[4]= {1, 2, 3, 4, 5}
In[5]:= NestWhileList[Function[Slot[1] + 1], 1, Function[True], 1, 4, 5]
Out[5]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}