Implementing more complex algorithms typically requires some kind of recursion or at least an iterative modification of a data structure. Appian supports both variants and in this post, I try to explain how and when to make use of them.
Iterative Algorithms
An iterative algorithm uses some kind of shared memory, which is modified during the iterations. When working with Appian, we typically use a!foreach() to work with lists. But as variables in Appian are immutable, thanks to it being a functional language, we cannot modify a variable defined outside our loop.
So, how do we implement an iterative algorithm with immutable memory? That’s simple. Create a new modified copy of the old one. The function reduce() does exactly that. Check the documentation here. Its job is to call a second function or expression for each item in the provided list. Similar to a!foreach(), but with a twist.
This function has a parameter called initial. The passed data can be of any type, including maps. Now, the data returned by each iteration is being passed to the next one for this parameter. This way, the called expression can store data and state in a data structure which is passed to itself.

Let’s have a look at an example. Create the two expressions AlgorithmIterative and AlgorithmIterativeHelper.
AlgorithmIterative()
reduce(
rule!AlgorithmIterativeHelper(
data:_,
item:_
),
a!map(
value: 1,
steps: {},
iterations: 0
),
1 + enumerate(10)
)
AlgorithmIterativeHelper(data(any), item(integer))
a!map(
value: ri!data.value * ri!Item,
steps: append(ri!data.steps, ri!data.value * ri!Item),
iterations: ri!data.iterations + 1
),
The map, defined as the initial value, is passed to the data parameter. The first item of the list created by calling enumerate() (1 .. 10) is passed to the item parameter. The helper expression creates another similar map and does some calculations. This map is then passed to the next iteration. The output looks like this:

Using this technique, you can easily implement algorithms like block-based checksum calculations or data partitioning.
Recursive Algorithms
In case you need a quick recap on what recursion is, please have a look at this and return once read. I’ll wait ….
… welcome back. So, we need to create an expression which calls itself while making sure to not get into an endless loop. I will pick the simple example of calculating the factorial of a given number.
Let’s see some C code example:
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
In Appian, this looks almost the same:
Fact(n(Integer))
if(
ri!n <= 1,
1,
ri!n * rule!Fact(ri!n - 1)
)

Summary
While a recursive approach has its drawbacks and is just harder to understand, it has one big advantage. You can end the recursion at any given time, while the iterative approach, in Appian, has a fixed number of iterations.
Both variants support passing a simple value or a complex data structure to the next iteration, acting as a shared memory. Keep in mind that this is not really shared, but a modified copy of the original data.
In my day-to-day work, I rarely use them, as we usually implement process-driven business applications without much requirement for more complex algorithms. And as I try to keep things simple, I typically prefer the iterative approach using the reduce() function.
I am curious which problem you solve using one of the approaches described.