Fold

Fold[f,x,list]
gives the last element of FoldList[f,x,list].

Fold[f,list]
is equivalent to Fold[f,First[list],Rest[list]].

Fold[f]
represents an operator form of Fold that can be applied to expressions.

DetailsDetails

  • You can use Throw to exit from Fold before it is finished.
  • Fold[f][list] is equivalent to Fold[f,list].
  • Fold[f][x,list] is equivalent to Fold[f,x,list].

ExamplesExamplesopen allclose all

Basic Examples  (7)Basic Examples  (7)

In[1]:=
Click for copyable input
Out[1]=
In[1]:=
Click for copyable input
Out[1]=
In[1]:=
Click for copyable input
Out[1]=
In[1]:=
Click for copyable input
Out[1]=

Start from the first element of the list:

In[1]:=
Click for copyable input
Out[1]=

Use the operator form of Fold on one argument:

In[1]:=
Click for copyable input
Out[1]=

Use the operator form of Fold on two arguments:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 1991
(2.0)
| Updated in 2016
(11.0)