Mutual recursion: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added/changed whitespace and comments, eliminated the need for a formatting function.)
Line 1,942:
=={{header|REXX}}==
===vanilla===
This version uses vertical formatting of the output.
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female seqsequence).*/
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('',20)
 
do j=0 to lim; jj=Jwright(j,w); ff=right(F(j),w); mm=right(M(j),w)
say say pad pad 'F('jj") =" Jw( ff) pad 'M('jj") =" Jw(mm)
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines─────────────────────*/
/*─────────────────────────────────────F, M, Jw subroutines────────────*/
F: procedure; parse arg n; if n==0 then return 1; return n - M(F(n-1))
M: procedure; parse arg n; if n==0 then return 0; return n - F(M(n-1))</lang>
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/</lang>
{{out}} using the default input of: &nbsp; <tt> 40 </tt>
<pre style="height:30ex">
Line 2,002 ⟶ 2,001:
This version uses memoization as well as a horizontal output format.
<br><br>The optimization due to memoization is faster by many orders of magnitude.
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female seqsequence).*/
parse arg lim .; if lim=='' then lim=99 40 /*getassume orthe assumedefault for LIM.? */
hmw=length(lim); $m.=; hm $m.0=0; hf $f.=; hf $f.0=1; Js=; Fs=; Ms=
 
do j=0 to lim; ff=F(j); mm=M(j)
Js=Js jWright(j,w); Fs=Fs jwright(ffF(j),w); Ms=Ms jWright(mmM(j),w)
end /*j*/
say 'Js=' Js /*display the list of Js to the term.*/
say 'Js=' Js
say 'Fs=' Fs /* " " " " Fs " " " */
say 'Fs=' Fs
say 'Ms=' Ms /* " " " " Ms " " " */
say 'Ms=' Ms
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines──────────────────────────────subroutines─────────────────────────────*/
F: procedure expose hm$m. hf$f.; parse arg n; if hf$f.n=='' then hf$f.n=n-M(F(n-1)); return hf$f.n
M: procedure expose hm$m. hf$f.; parse arg n; if hm$m.n=='' then hm$m.n=n-F(M(n-1)); return hm$m.n</lang>
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/</lang>
{{out}} using the default input of: &nbsp; <tt> 99 </tt>
<pre>
Line 2,027 ⟶ 2,025:
This version is identical in function to the previous example, but it also can compute and
<br>display a specific request (indicated by a negative number for the argument).
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female seqsequence).*/
/*If LIM is negative, only show a single result for the abs(lim) entry.*/
 
parse arg lim .; if lim=='' then lim=99; aLim=abs(lim)
parsew=length(aLim); var lim . hm. hf. Js Fs Ms$m.=; hm$m.0=0; $f.=; hf $f.0=1; Js=; Fs=; Ms=
 
do j=0 to Alim; ff=F(j); mm=M(j)
Js=Js jWright(j,w); Fs=Fs jwright(ffF(j),w); Ms=Ms jWright(mmM(j),w)
end end/*j*/
 
if lim>0 then say 'Js=' Js; else say 'J('aLim")=" word(Js,aLim+1)
if lim>0 then say 'Fs=' Fs; else say 'F('aLim")=" word(Fs,aLim+1)
if lim>0 then say 'Ms=' Ms; else say 'M('aLim")=" word(Ms,aLim+1)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines──────────────────────────────subroutines─────────────────────────────*/
F: procedure expose hm$m. hf$f.; parse arg n; if hf$f.n=='' then hf$f.n=n-M(F(n-1)); return hf$f.n
M: procedure expose hm$m. hf$f.; parse arg n; if hm$m.n=='' then hm$m.n=n-F(M(n-1)); return hm$m.n</lang>
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/</lang>
{{out}} using the input of: &nbsp; <tt> -70000 </tt>
<pre>