Mutual recursion: Difference between revisions
Content deleted Content added
added MiniScript example |
m →{{header|REXX}}: optimized all three programs, added whitespace, used templates for the output sections. |
||
Line 2,542: | Line 2,542: | ||
This version uses vertical formatting of the output. |
This version uses vertical formatting of the output. |
||
<lang rexx>/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */ |
<lang rexx>/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */ |
||
parse arg lim .; |
parse arg lim .; if lim='' then lim= 40; w= length(lim); pad= left('', 20) |
||
do j=0 |
do j=0 for lim+1; jj= right(j, w); ff= right(F(j), w); mm= right(M(j), w) |
||
say pad 'F('jj") =" ff pad 'M('jj") =" |
say pad 'F('jj") =" ff pad 'M('jj") =" mm |
||
end /*j*/ |
end /*j*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
Line 2,551: | Line 2,551: | ||
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) )</lang> |
M: procedure; parse arg n; if n==0 then return 0; return n - F( M(n-1) )</lang> |
||
{{out|output|text= when using the default input of: <tt> 40 </tt>}} |
|||
<pre> |
<pre> |
||
F( 0) = 1 M( 0) = 0 |
F( 0) = 1 M( 0) = 0 |
||
Line 2,601: | Line 2,601: | ||
<lang rexx>/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */ |
<lang rexx>/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */ |
||
parse arg lim .; if lim=='' then lim=40 /*assume the default for LIM? */ |
parse arg lim .; if lim=='' then lim=40 /*assume the default for LIM? */ |
||
w=length(lim); |
w= length(lim); $m.=.; $m.0= 0; $f.=.; $f.0= 1; Js=; Fs=; Ms= |
||
do j=0 |
do j=0 for lim+1 |
||
Js=Js right(j, w); |
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 /*display the list of Js to the term.*/ |
||
Line 2,611: | Line 2,611: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
F: procedure expose $m. $f.; parse arg 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 $m. $f.; parse arg n; |
M: procedure expose $m. $f.; parse arg n; if $m.n==. then $m.n= n-F(M(n-1)); return $m.n</lang> |
||
{{out|output|text= when using the default input of: <tt> 99 </tt>}} |
|||
<pre> |
<pre> |
||
Js= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
Js= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
||
Line 2,626: | Line 2,626: | ||
/*───────────────── If LIM is negative, a single result is shown for the abs(lim) entry.*/ |
/*───────────────── If LIM is negative, a single result is shown 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; |
w= length(aLim); $m.=.; $m.0= 0; $f.=.; $f.0= 1; Js=; Fs=; Ms= |
||
do j=0 |
do j=0 for Alim+1 |
||
Js=Js right(j, w); |
Js= Js right(j, w); Fs= Fs right( F(j), w); Ms= Ms right( M(j), w) |
||
end /*j*/ |
end /*j*/ |
||
Line 2,638: | Line 2,638: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
F: procedure expose $m. $f.; parse arg 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 $m. $f.; parse arg n; |
M: procedure expose $m. $f.; parse arg n; if $m.n==. then $m.n= n-F(M(n-1)); return $m.n</lang> |
||
{{out|output|text= when using the input of: <tt> -70000 </tt>}} |
|||
<pre> |
<pre> |
||
J(70000)= 70000 |
J(70000)= 70000 |
||
Line 2,646: | Line 2,646: | ||
M(70000)= 43262 |
M(70000)= 43262 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the input of a negative <big>¼</big> million: <tt> -250000 </tt>}} |
|||
<pre> |
<pre> |
||
J(250000)= 250000 |
J(250000)= 250000 |