accumulate(f, x, L)
accumulate(f, L)
accumulate(L, x, f)
accumulate(L, f)
Suppose L=\{x0, x1, ..., xn\}. Then for any binary operator f, accumulate(f, L) returns the list \{f(x0, x1), 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 accumulate(f, x, L) is called, the element x is used as the first argument of the binary function f. In other words, accumulate(f, \{x0, x1, \ldots, xn\}) is equivalent to accumulate(f, x0, \{x1, \ldots, xn\}).
|
|
The function accumulate(\{x_0, x_1, \ldots, x_n\}, f) returns the list \{..., f(x_{n-2}, f(x_{n-1}, x_n)), f(x_{n-1}, x_n) \} . That is, f is applied to the last two elements of the list, and the result placed at the end of the output. Then the accumulation proceeds backwards through the list. The optional argument x in accumulate(L, x, f) is used as the second argument in the first evaluation of f. So accumulate(\{x_0, x_1, \ldots, x_{n-1}\}, x_n, f) is equivalent to accumulate(\{x_0, x_1, \ldots, x_n\}, f).
|
|
|
If L is an instance of class the the iterator method installed, e.g., a string, then it may also be used with accumulate, but only the versions with f as the first argument. Its return value in this case is an Iterator object.
|
|
|
|
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 accumulate is a method function.