This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Function Names as ExpressionsApplying Functions to Lists and Other Expressions

2.2.2 Applying Functions Repeatedly

Many programs you write will involve operations that need to be iterated several times. Nest and NestList are powerful constructs for doing this.

Applying functions of one argument repeatedly.

Nest[f, x, n] takes the "name" f of a function, and applies the function n times to x.

In[1]:= Nest[f, x, 4]

Out[1]=

This makes a list of each successive nesting.

In[2]:= NestList[f, x, 4]

Out[2]=

Here is a simple function.

In[3]:= recip[x_] := 1/(1 + x)

You can iterate the function using Nest.

In[4]:= Nest[recip, x, 3]

Out[4]=

Nest and NestList allow you to apply functions a fixed number of times. Often you may want to apply functions until the result no longer changes. You can do this using FixedPoint and FixedPointList.

Applying functions until the result no longer changes.

Here is a function that takes one step in Newton's approximation to .

In[5]:= newton3[x_] := N[ 1/2 ( x + 3/x ) ]

Here are five successive iterates of the function, starting at .

In[6]:= NestList[newton3, 1.0, 5]

Out[6]=

Using the function FixedPoint, you can automatically continue applying newton3 until the result no longer changes.

In[7]:= FixedPoint[newton3, 1.0]

Out[7]=

Here is the sequence of results.

In[8]:= FixedPointList[newton3, 1.0]

Out[8]=

Applying functions repeatedly until a test fails.

Here is a function which divides a number by 2.

In[9]:= divide2[n_] := n/2

This repeatedly applies divide2 until the result is no longer an even number.

In[10]:= NestWhileList[divide2, 123456, EvenQ]

Out[10]=

This repeatedly applies newton3, stopping when two successive results are no longer considered unequal, just as in FixedPointList.

In[11]:= NestWhileList[newton3, 1.0, Unequal, 2]

Out[11]=

This goes on until the first time a result that has been seen before reappears.

In[12]:= NestWhileList[Mod[5 #, 7]&, 1, Unequal, All]

Out[12]=

Operations such as Nest take a function f of one argument, and apply it repeatedly. At each step, they use the result of the previous step as the new argument of f.

It is important to generalize this notion to functions of two arguments. You can again apply the function repeatedly, but now each result you get supplies only one of the new arguments you need. A convenient approach is to get the other argument at each step from the successive elements of a list.

Ways to repeatedly apply functions of two arguments.

Here is an example of what FoldList does.

In[13]:= FoldList[f, x, {a, b, c}]

Out[13]=

Fold gives the last element of the list produced by FoldList.

In[14]:= Fold[f, x, {a, b, c}]

Out[14]=

This gives a list of cumulative sums.

In[15]:= FoldList[Plus, 0, {a, b, c}]

Out[15]=

Using Fold and FoldList you can write many elegant and efficient programs in Mathematica. In some cases, you may find it helpful to think of Fold and FoldList as producing a simple nesting of a family of functions indexed by their second argument.

This defines a function nextdigit.

In[16]:= nextdigit[a_, b_] := 10 a + b

Here is a rather elegant definition of a function that gives the number corresponding to a list of digits in base 10.

In[17]:= tonumber[digits_] := Fold[nextdigit, 0, digits]

Here is an example of tonumber in action.

In[18]:= tonumber[{1, 3, 7, 2, 9, 1}]

Out[18]=

Function Names as ExpressionsApplying Functions to Lists and Other Expressions