Skip to content

Fold

Fold[f, x, {a, b}] returns f[f[x, a], b], and this nesting continues for lists of arbitrary length. Fold[f, {a, b, c}] returns Fold[f, a, {b, c}]. Fold[f] is an operator form that can be applied to expressions such as {a, b, c}.

Attributes[Fold] := {Protected}

Simple examples

In[1]:= Fold[f, 1, {2, 3}]
Out[1]= f[f[1, 2], 3]
In[2]:= Fold[f, {1, 2, 3}]
Out[2]= f[f[1, 2], 3]
In[3]:= Fold[f, e1, h[e2, e3, e4]]
Out[3]= f[f[f[e1, e2], e3], e4]
In[4]:= Fold[f, h, {}]
Out[4]= h