How to resolve the algorithm Anonymous recursion step by step in the Seed7 programming language
How to resolve the algorithm Anonymous recursion step by step in the Seed7 programming language
Table of Contents
Problem Statement
While implementing a recursive function, it often happens that we must resort to a separate helper function to handle the actual recursion. This is usually the case when directly calling the current function would waste too many resources (stack space, execution time), causing unwanted side-effects, and/or the function doesn't have the right arguments and/or return values. So we end up inventing some silly name like foo2 or foo_helper. I have always found it painful to come up with a proper name, and see some disadvantages: Some languages allow you to embed recursion directly in-place. This might work via a label, a local gosub instruction, or some special keyword. Anonymous recursion can also be accomplished using the Y combinator.
If possible, demonstrate this by writing the recursive version of the fibonacci function (see Fibonacci sequence) which checks for a negative argument before doing the actual recursion.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Anonymous recursion step by step in the Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
const func integer: fib (in integer: x) is func
result
var integer: fib is 0;
local
const func integer: fib1 (in integer: n) is func
result
var integer: fib1 is 0;
begin
if n < 2 then
fib1 := n;
else
fib1 := fib1(n-2) + fib1(n-1);
end if;
end func;
begin
if x < 0 then
raise RANGE_ERROR;
else
fib := fib1(x);
end if;
end func;
const proc: main is func
local
var integer: i is 0;
begin
for i range 0 to 4 do
writeln(fib(i));
end for;
end func;
You may also check:How to resolve the algorithm Binary digits step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the BCPL programming language
You may also check:How to resolve the algorithm Forward difference step by step in the zkl programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the C++ programming language
You may also check:How to resolve the algorithm Infinity step by step in the PicoLisp programming language