AccumulateSteps
Computes a value by accumulating the values in a container and returns a container of the same type with every partial value obtained in the process.
Syntax
-
AccumulateSteps(X, x, f)
-
X
is an ordered container -
x
is any object -
f
is a function
-
Description
If X
is an ordered container, x
a value, and f
a function, then AccumulateSteps(X, x, f)
returns a container of the same type as X
containing the partially accumulated values in X
using x
as the initial value and f
as the function. Specifically, AccumulateSteps(X, x, f)
returns the container Y
of size n
defined by
Y[1] ≔ f( x , X[1]) Y[2] ≔ f(Y[1], X[2]), Y[3] ≔ f(Y[2], X[3]), ⋮ Y[n] ≔ f(Y[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)
AccumulateSteps(X, 0, add)
(5, 6, 8, 2, 6, 8, 9, 10, 4)
AccumulateSteps(X, 1, multiply)
(5, 5, 10, −60, −240, −480, −480, −480, 2880)
AccumulateSteps(X, 1, lcm)
(5, 5, 10, 30, 60, 60, 60, 60, 60)
A ≔ ❨❨2, 4, 3, −2, 4❩, ❨4, 2, −3, 6, −8❩, ❨4, 2, 1, −3, 5❩, ❨4, 8, −7, 1, 2❩, ❨6, 3, 2, −2, 10❩❩
⎛ 2 4 3 −2 4⎞ ⎜ 4 2 −3 6 −8⎟ ⎜ 4 2 1 −3 5⎟ ⎜ 4 8 −7 1 2⎟ ⎝ 6 3 2 −2 10⎠
AccumulateSteps(A, 0, add)
⎛ 2 6 9 7 11⎞ ⎜15 17 14 20 12⎟ ⎜16 18 19 16 21⎟ ⎜25 33 26 27 29⎟ ⎝35 38 40 38 48⎠
AccumulateSteps(A, 1, lcm)
⎛ 2 4 12 12 12⎞ ⎜ 12 12 12 12 24⎟ ⎜ 24 24 24 24 120⎟ ⎜120 120 840 840 840⎟ ⎝840 840 840 840 840⎠
AccumulateSteps(A, 0, max)
⎛ 2 4 4 4 4⎞ ⎜ 4 4 4 6 6⎟ ⎜ 6 6 6 6 6⎟ ⎜ 6 8 8 8 8⎟ ⎝ 8 8 8 8 10⎠