# Folding operators: foldl foldr

### 5.40.31 Folding operators: foldl foldr

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 an arbitrary number 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).

#### Example.

Input:

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

Output:

32768

which is 2(35)

- foldr takes an arbitrary number 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))))

#### Example.

Input:

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

Output:

847288609443

which is 3(52)