Hello.
I was experimenting with action blocks and I noticed how all their variables (input, work and output) have a default value and not just a current value. Could this mean that, contrary to macro variables, action block variables are reinstantiated from their default value each time a block is called? That would allow for straightforward recursion!
With that in mind, I tried to write simple blocks to calculate Factorial (yes, I know there is the FACT() function, which is much faster and doesn't crash if n gets a little higher, but what the hell) and the Fibonacci sequence, which are basic examples of recursion. And yes! It works. I checked the log and could see that, each time a block gets a nested call, its variables change value from their defaults (0 in these cases), not from their current value in the outer call – that is, new instances are created. Cool! (Did I just write that?)
Maybe everybody already new this but I didnt't, so here are my two functions. Enjoy.
P.S. – What isn't shown here is where the output.result variable goes when the blocks call themselves. Well, in the case of Factorial, it goes into work.prev_res; in Fibonacci there are two calls to itself and, obviously, in the first call output.result goes into work.prev_res1 and in the second it goes into work.prev_res2.
P.P.S. – I also wrote short descriptions for both functions/blocks but they weren't included by Share as Image. So I'll copy them here:
Factorial: Calculates the factorial of n for n >= 0. If n < 0 signals an error as -1.
Fibonacci: Calculates the n.th member of the Fibonacci sequence for n >= 1, where the first two members are 1,2. If n < 1 signals an error as -1.
P.P.P.S. – Actually, there was a slight bug in the Fibonacci function, though it made no difference to the resulting value. In the calls to itself, the variables work.prev_n2 and work.prev_n1, which are assigned to input.n, are the other way around: work.prev_n1 should be used in the first call and work.prev_n2 in the second.
I was experimenting with action blocks and I noticed how all their variables (input, work and output) have a default value and not just a current value. Could this mean that, contrary to macro variables, action block variables are reinstantiated from their default value each time a block is called? That would allow for straightforward recursion!
With that in mind, I tried to write simple blocks to calculate Factorial (yes, I know there is the FACT() function, which is much faster and doesn't crash if n gets a little higher, but what the hell) and the Fibonacci sequence, which are basic examples of recursion. And yes! It works. I checked the log and could see that, each time a block gets a nested call, its variables change value from their defaults (0 in these cases), not from their current value in the outer call – that is, new instances are created. Cool! (Did I just write that?)
Maybe everybody already new this but I didnt't, so here are my two functions. Enjoy.
P.S. – What isn't shown here is where the output.result variable goes when the blocks call themselves. Well, in the case of Factorial, it goes into work.prev_res; in Fibonacci there are two calls to itself and, obviously, in the first call output.result goes into work.prev_res1 and in the second it goes into work.prev_res2.
P.P.S. – I also wrote short descriptions for both functions/blocks but they weren't included by Share as Image. So I'll copy them here:
Factorial: Calculates the factorial of n for n >= 0. If n < 0 signals an error as -1.
Fibonacci: Calculates the n.th member of the Fibonacci sequence for n >= 1, where the first two members are 1,2. If n < 1 signals an error as -1.
P.P.P.S. – Actually, there was a slight bug in the Fibonacci function, though it made no difference to the resulting value. In the calls to itself, the variables work.prev_n2 and work.prev_n1, which are assigned to input.n, are the other way around: work.prev_n1 should be used in the first call and work.prev_n2 in the second.
Last edited: