Algosim documentation: AccumulateList

AccumulateList

Computes a value by accumulating the values in a container and returns a list with every partial value obtained in the process.

Syntax

Description

If X is an ordered container, x a value, and f a function, then AccumulateList(X, x, f) returns the list containing the partially accumulated values in X using x as the initial value and f as the function. Specifically, AccumulateList(X, x, f) returns the list L of length n defined by

L[1] ≔ f( x  , X[1])
L[2] ≔ f(L[1], X[2]),
L[3] ≔ f(L[2], X[3]),
⋮
L[n] ≔ f(L[n−1], X[n])

where n = #X.

Examples

X ≔ ❨5, 1, 2, −6, 4, 2, 1, 1, −6❩
(5, 1, 2, −6, 4, 2, 1, 1, −6)
AccumulateList(X, 0, add)
5
6
8
2
6
8
9
10
4
AccumulateList(X, 1, multiply)
5
5
10
−60
−240
−480
−480
−480
2880
AccumulateList(X, 1, lcm)
5
5
10
30
60
60
60
60
60

See also