fold(f, x, L)
fold(f, L)
fold(L, x, f)
fold(L, f)
Suppose L=\{x0, x1, ..., xn\}. Then for any binary operator f, fold(f, L) computes f(...f(f(x0, x1), x2), ...). In other words, the binary operator is applied to the first two elements of L, then to that result along with the next unused element of L, and so forth.



If fold(f, x, L) is called, the element x is used as the first argument of the binary function f. In other words, fold(f, \{x0, x1, \ldots, xn\}) is equivalent to fold(f, x0, \{x1, \ldots, xn\}).


The function fold(\{x_0, x_1, \ldots, x_n\}, f) returns f...f(f(x_{n2}, f(x_{n1}, x_n))). That is, f is applied to the last two elements of the list first, then the repeated calls to f proceed backwards through the list. The optional argument x in fold(L, x, f) is used as the second argument in the first evaluation of f. So fold(\{x_0, x_1, \ldots, x_{n1}\}, x_n, f) is equivalent to fold(\{x_0, x_1, \ldots, x_n\}, f).



The difference between fold and accumulate is that fold returns the final result of all the nested evaluations of f, while accumulate lists all the intermediate values as well.

The object fold is a method function.