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: Line 1,942:
=={{header|REXX}}==
=={{header|REXX}}==
===vanilla===
===vanilla===
This version uses vertical formatting.
This version uses vertical formatting of the output.
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
parse arg lim .; if lim='' then lim=40; pad=left('',20)
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('',20)


do j=0 to lim; jj=Jw(j); ff=F(j); mm=M(j)
do j=0 to lim; jj=right(j,w); ff=right(F(j),w); mm=right(M(j),w)
say pad 'F('jj") =" Jw(ff) pad 'M('jj") =" Jw(mm)
say pad 'F('jj") =" ff pad 'M('jj") =" mm
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
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))
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))
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>
{{out}} using the default input of: &nbsp; <tt> 40 </tt>
<pre style="height:30ex">
<pre style="height:30ex">
Line 2,002: Line 2,001:
This version uses memoization as well as a horizontal output format.
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.
<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 seq)*/
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
parse arg lim .; if lim=='' then lim=99 /*get or assume LIM.*/
parse arg lim .; if lim=='' then lim=40 /*assume the default for LIM? */
hm.=; hm.0=0; hf.=; hf.0=1; Js=; Fs=; Ms=
w=length(lim); $m.=; $m.0=0; $f.=; $f.0=1; Js=; Fs=; Ms=


do j=0 to lim; ff=F(j); mm=M(j)
do j=0 to lim
Js=Js jW(j); Fs=Fs jw(ff); Ms=Ms jW(mm)
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w)
end /*j*/
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 done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines──────────────────────────────*/
/*──────────────────────────────────one─liner subroutines─────────────────────────────*/
F: procedure expose hm. hf.; parse arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
F: procedure expose $m. $f.; parse arg n; if $f.n=='' then $f.n=n-M(F(n-1)); return $f.n
M: procedure expose hm. hf.; parse arg n; if hm.n=='' then hm.n=n-F(M(n-1)); return hm.n
M: procedure expose $m. $f.; parse arg n; if $m.n=='' then $m.n=n-F(M(n-1)); return $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>
{{out}} using the default input of: &nbsp; <tt> 99 </tt>
<pre>
<pre>
Line 2,027: Line 2,025:
This version is identical in function to the previous example, but it also can compute and
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).
<br>display a specific request (indicated by a negative number for the argument).
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
/*If LIM is negative, only show a single result for the abs(lim) entry.*/
/*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)
parse arg lim .; if lim=='' then lim=99; aLim=abs(lim)
parse var lim . hm. hf. Js Fs Ms; hm.0=0; hf.0=1
w=length(aLim); $m.=; $m.0=0; $f.=; $f.0=1; Js=; Fs=; Ms=


do j=0 to Alim; ff=F(j); mm=M(j)
do j=0 to Alim
Js=Js jW(j); Fs=Fs jw(ff); Ms=Ms jW(mm)
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(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 '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 '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)
if lim>0 then say 'Ms=' Ms; else say 'M('aLim")=" word(Ms,aLim+1)
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines──────────────────────────────*/
/*──────────────────────────────────one─liner subroutines─────────────────────────────*/
F: procedure expose hm. hf.; parse arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
F: procedure expose $m. $f.; parse arg n; if $f.n=='' then $f.n=n-M(F(n-1)); return $f.n
M: procedure expose hm. hf.; parse arg n; if hm.n=='' then hm.n=n-F(M(n-1)); return hm.n
M: procedure expose $m. $f.; parse arg n; if $m.n=='' then $m.n=n-F(M(n-1)); return $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>
{{out}} using the input of: &nbsp; <tt> -70000 </tt>
<pre>
<pre>