I've come across a need to define something like a "function" but with persistent state. That is, each time the "function" is called, a parameter updates, so that the next time it is called it performs a different operation.

I've come up with some form of notation for this, but was wondering if there's anything more standard.

For example, I'd like to define a "difference" "function", which returns the difference of inputs between calls, with the value of the previous input initialized to 0.

\begin{align} &\Delta: x \mapsto y; \text{ Persistent: } x_{last}\leftarrow 0 \\ &y \leftarrow x-x_{last} \\ &x_{last} = x \\ \end{align}

Or a "running sum" function, which adds the input to an persistent "sum" variable and returns that:

\begin{align} &\Sigma: x \mapsto y; \text{ Persistent: } y\leftarrow 0 \\ &y \leftarrow y-x \\ \end{align}

Using this notation I'd later like to make simpilifications such as: $(\Sigma \circ \Delta)(x_t) = x_t$ (Because $y_0 + (x_1-x_0) + (x_2-x_1) + ... + (x_t-x_{t-1}) |_{x_0=0, y_0=0} = x_t$).

Or describe chains of operations, in which each step returns one variable and updates its internal state, for example:

$$ y_t = (f_L \circ ... \circ f_1)(x_t) $$

So, is there a widely accepted notation for this kind of thing, or should I just define it as I already have?


1 Answer 1


For the running sum function, one approach could be to define a function $S$ that takes an infinite sequence of real numbers $(x_1,x_2,\ldots)$ as input and returns the sequence $(x_1, x_1+x_2,x_1+x_2+x_3,\ldots)$ as output. The difference function $D$ could be handled similarly. We would have identities like $D \circ S = I$, where $I$ is the identity operator.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .