How to resolve the algorithm Mutual recursion step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mutual recursion step by step in the uBasic/4tH 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 uBasic/4tH programming language

Source code in the ubasic/4th programming language

LOCAL(1)                               ' main uses locals as well

FOR a@ = 0 TO 200                      ' set the array
  @(a@) = -1
NEXT

PRINT "F sequence:"                    ' print the F-sequence
FOR a@ = 0 TO 20
  PRINT FUNC(_f(a@));" ";
NEXT
PRINT

PRINT "M sequence:"                    ' print the M-sequence
FOR a@ = 0 TO 20
  PRINT FUNC(_m(a@));" ";
NEXT
PRINT

END


_f PARAM(1)                            ' F-function
  IF a@ = 0 THEN RETURN (1)            ' memoize the solution
  IF @(a@) < 0 THEN @(a@) = a@ - FUNC(_m(FUNC(_f(a@ - 1))))
RETURN (@(a@))                         ' return array element


_m PARAM(1)                            ' M-function
  IF a@ = 0 THEN RETURN (0)            ' memoize the solution
  IF @(a@+100) < 0 THEN @(a@+100) = a@ - FUNC(_f(FUNC(_m(a@ - 1))))
RETURN (@(a@+100))                     ' return array element

  

You may also check:How to resolve the algorithm Fractal tree step by step in the Haskell programming language
You may also check:How to resolve the algorithm Abelian sandpile model step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Forth programming language
You may also check:How to resolve the algorithm Compare length of two strings step by step in the REXX programming language
You may also check:How to resolve the algorithm Substring step by step in the Maxima programming language