Mutual recursion: Difference between revisions

Added uBasic/4tH version
(J: voice an issue with task concept)
(Added uBasic/4tH version)
Line 2,499:
f(15) = 9; m(15) = 9</pre>
 
=={{header|uBasic/4tH}}==
{{trans|BBC BASIC}}
uBasic/4tH supports mutual recursion. However, the underlying system can't support the stress this puts on the stack - at least not for the full sequence. This version uses [https://en.wikipedia.org/wiki/Memoization memoization] to alleviate the stress and speed up execution.
<lang>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</lang>
{{out}}
<pre>F sequence:
1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13
M sequence:
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12
 
0 OK, 0:199</pre>
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
374

edits