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 |
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/ |
||
parse arg lim .; |
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('',20) |
||
do j=0 to lim; jj=right(j,w); ff=right(F(j),w); mm=right(M(j),w) |
|||
say pad 'F('jj") =" ff pad 'M('jj") =" mm |
|||
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: |
F: procedure; parse arg n; if n==0 then return 1; return n - M(F(n-1)) |
||
M: |
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: <tt> 40 </tt> |
{{out}} using the default input of: <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 |
<lang rexx>/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/ |
||
parse arg lim .; if lim=='' then lim= |
parse arg lim .; if lim=='' then lim=40 /*assume the default for LIM? */ |
||
w=length(lim); $m.=; $m.0=0; $f.=; $f.0=1; Js=; Fs=; Ms= |
|||
do j=0 to lim |
|||
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(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 done.*/ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────one─liner |
/*──────────────────────────────────one─liner subroutines─────────────────────────────*/ |
||
F: |
F: procedure expose $m. $f.; parse arg n; if $f.n=='' then $f.n=n-M(F(n-1)); return $f.n |
||
M: |
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: <tt> 99 </tt> |
{{out}} using the default input of: <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 |
<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) |
||
w=length(aLim); $m.=; $m.0=0; $f.=; $f.0=1; Js=; Fs=; Ms= |
|||
do j=0 to Alim |
|||
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w) |
|||
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 |
/*──────────────────────────────────one─liner subroutines─────────────────────────────*/ |
||
F: |
F: procedure expose $m. $f.; parse arg n; if $f.n=='' then $f.n=n-M(F(n-1)); return $f.n |
||
M: |
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: <tt> -70000 </tt> |
{{out}} using the input of: <tt> -70000 </tt> |
||
<pre> |
<pre> |