How to resolve the algorithm Mutual recursion step by step in the Oberon-2 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Mutual recursion step by step in the Oberon-2 programming language
Table of Contents
Problem Statement
Two functions are said to be mutually recursive if the first calls the second, and in turn the second calls the first. Write two mutually recursive functions that compute members of the Hofstadter Female and Male sequences defined as:
(If a language does not allow for a solution using mutually recursive functions then state this rather than give a solution by other means).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mutual recursion step by step in the Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE MutualRecursion;
IMPORT Out;
TYPE
Fn = PROCEDURE(n:INTEGER):INTEGER;
PROCEDURE^ M(n:INTEGER):INTEGER;
PROCEDURE F(n:INTEGER):INTEGER;
BEGIN
IF n=0 THEN RETURN 1
ELSE RETURN n-M(F(n-1))
END;
END F;
PROCEDURE M(n:INTEGER):INTEGER;
BEGIN
IF n=0 THEN RETURN 0
ELSE RETURN n-F(M(n-1))
END;
END M;
(* Print the first few values of one of the functions *)
PROCEDURE Show(name:ARRAY OF CHAR;fn:Fn);
CONST Max = 15;
VAR i:INTEGER;
BEGIN
Out.String(name);
Out.String(": ");
FOR i := 0 TO Max DO
Out.Int(fn(i),0);
Out.String(" ");
END;
Out.Ln;
END Show;
(* Show the first values of both F and M *)
BEGIN
Show("F", F);
Show("M", M);
END MutualRecursion.
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the Delphi programming language
You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Haskell programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the D programming language