Skip to main content
added 67 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

I'm going to answer this because it provides an opportunity to show a simple method for examining the behavior of the test function. First the syntax from the documentation:

Mathematica graphics Mathematica graphics

So let's try:

NestWhileList[# + 1 &, 0, (Print[#, " ", #2]; True) &, 2, 5] 

0 1

1 2

2 3

3 4

{0, 1, 2, 3, 4, 5} 

ThisThe function (Print[#, " ", #2]; True) & lets you easily see what is being passed to the texttest function at each step, and automatically aborts after five steps because of the last argument.

Here is another example. We end the loop when a certain ratio is exceeded:

NestWhileList[# + 1 &, 0, (Print["prev: ", #, " current: ", #2]; 1 - #/#2 > 0.15) &, 2] 

Mathematica graphics

{0, 1, 2, 3, 4, 5, 6, 7} 

Hopefully this method and example will help you work these things out for yourself more easily.

I'm going to answer this because it provides an opportunity to show a simple method for examining the behavior of the test function. First the syntax from the documentation:

Mathematica graphics Mathematica graphics

So let's try:

NestWhileList[# + 1 &, 0, (Print[#, " ", #2]; True) &, 2, 5] 

0 1

1 2

2 3

3 4

{0, 1, 2, 3, 4, 5} 

This lets you easily see what is being passed to the text function at each step, and automatically aborts after five steps.

Here is another example. We end the loop when a certain ratio is exceeded:

NestWhileList[# + 1 &, 0, (Print["prev: ", #, " current: ", #2]; 1 - #/#2 > 0.15) &, 2] 

Mathematica graphics

{0, 1, 2, 3, 4, 5, 6, 7} 

Hopefully this method and example will help you work these things out for yourself more easily.

I'm going to answer this because it provides an opportunity to show a simple method for examining the behavior of the test function. First the syntax from the documentation:

Mathematica graphics Mathematica graphics

So let's try:

NestWhileList[# + 1 &, 0, (Print[#, " ", #2]; True) &, 2, 5] 

0 1

1 2

2 3

3 4

{0, 1, 2, 3, 4, 5} 

The function (Print[#, " ", #2]; True) & lets you easily see what is being passed to the test function at each step, and automatically aborts after five steps because of the last argument.

Here is another example. We end the loop when a certain ratio is exceeded:

NestWhileList[# + 1 &, 0, (Print["prev: ", #, " current: ", #2]; 1 - #/#2 > 0.15) &, 2] 

Mathematica graphics

{0, 1, 2, 3, 4, 5, 6, 7} 

Hopefully this method and example will help you work these things out for yourself more easily.

Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

I'm going to answer this because it provides an opportunity to show a simple method for examining the behavior of the test function. First the syntax from the documentation:

Mathematica graphics Mathematica graphics

So let's try:

NestWhileList[# + 1 &, 0, (Print[#, " ", #2]; True) &, 2, 5] 

0 1

1 2

2 3

3 4

{0, 1, 2, 3, 4, 5} 

This lets you easily see what is being passed to the text function at each step, and automatically aborts after five steps.

Here is another example. We end the loop when a certain ratio is exceeded:

NestWhileList[# + 1 &, 0, (Print["prev: ", #, " current: ", #2]; 1 - #/#2 > 0.15) &, 2] 

Mathematica graphics

{0, 1, 2, 3, 4, 5, 6, 7} 

Hopefully this method and example will help you work these things out for yourself more easily.