[Oberon] Recursion of inner procedures
August Karlstrom
fusionfile at gmail.com
Thu Dec 14 09:17:15 CET 2023
> On 2023-12-12 23:30, Diego Sardina wrote:
> Recently I wrote to Wirth about this and he confirmed that inner
> procedures can call themselves.
>
> How you would implement it? Introducing the procedure identifier in
> the local scope with the possibility of shadowing it by another local
> declaration? Of course in a clean way :-)
We can maintain a stack of procedure identifiers which reflects the
current procedure nesting. On a procedure call we look for the procedure
identifier
1. in the current scope
2. on the top of the stack
3. in the global scope.
Then a procedure like this will behave as expected:
PROCEDURE Sum(a: ARRAY OF INTEGER; n: INTEGER): INTEGER;
PROCEDURE Sum(a: ARRAY OF INTEGER; n, s: INTEGER): INTEGER;
VAR result: INTEGER;
BEGIN
IF n > 0 THEN
result := Sum(a, n - 1, s + a[n - 1])
ELSE
result := s
END
RETURN result
END Sum;
RETURN Sum(a, n, 0)
END Sum
However, it may be more clear to use a distinct name for the local
procedure.
August
More information about the Oberon
mailing list