Folding sequences

### 6.3.30 Folding sequences

The foldl (left-fold) and foldr (right-fold) commands take an infixed operator or function of two variables and apply them across a sequence of inputs through left and right association.

- foldl takes a sequence of arguments:
  - R, an infixed operator or function of two variables.
  - I, an initial value.
  - a1,a2,…, ak, an arbitrary number of additional arguments.
- foldl(R,I,a1,…,ak) returns R(… R(R(I,a1),a2)…,ak).
- foldr takes a sequence of arguments:
  - R, an infixed operator or function of two variables.
  - I, an initial value.
  - a1,a2,…,aj, an arbitrary number of additional arguments.
- foldr(R,I,a1,a2,…,ak) returns R(a,…(R(a1,R(a2,… R(ak−1,R(ak,I)))).

#### Examples

foldl('^',2,3,5)

32768 |  |  |  |  |  |  |  |  |  |

which is equal to 2(35).

foldr('^',2,3,5)

847288609443 |  |  |  |  |  |  |  |  |  |

which is equal to 3(52).