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.

Details

  • 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].

Examples

open allclose all

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]=

Scope  (2)

Generalizations & Extensions  (3)

Applications  (9)

Properties & Relations  (3)

Possible Issues  (1)

Neat Examples  (3)

See Also

Nest  FoldList  FoldPair  ParallelCombine

Tutorials

Introduced in 1991
(2.0)
| Updated in 2016
(11.0)