This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Functional Programming  /
Fold

  • Fold[ f , x , list ] gives the last element of FoldList[ f , x , list ].
  • Example: Fold[f, x, a, b, c ].
  • You can use Throw to exit from Fold before it is finished.
  • See notes for FoldList.
  • See the Mathematica book: Section 2.2.2.
  • See also: Nest.

    Further Examples

    (See the Further Examples for ReplacePart for a powerful application of Fold to partial evaluation of held expressions.)
    Here is the result of folding the function f over the list {a,b,c} starting with x.

    In[1]:=

    Out[1]=

    Here is a definition of the factorial function in a functional style.

    In[2]:=

    In[3]:=

    Out[3]=

    In[4]:=

    Here is an implementation of Horner's rule.

    In[5]:=

    Out[5]=