Previous Up Next

5.43.38  Fold operators: foldl, foldr

The fold operators foldl and foldr both take a binary operator, identifier or function R as the first argument followed by an argument I and an arbitrary number of arguments a, b, c, …, like for example foldl(R,I,a,b,c,...).

The left-fold operator foldl composes a binary operator R with initial value I onto the arguments a, b, …(which may be zero in number), associating from the left. For example, input:

foldl(R,I,a,b,c)

Output:

R(R(R(I,a),b),c)

The right-fold operator foldr is similar but associates operands from the right. For example, input:

foldr(R,I,a,b,c)

Output:

R(a,R(b,R(c,I)))

Previous Up Next