Mutual recursion: Difference between revisions
Content added Content deleted
m (→{{header|Fortran}}: grammar fix + a note) |
(gnu octave) |
||
Line 125: | Line 125: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Octave}}== |
|||
We don't need to pre-declare or specify in some other way a function that will be defined later. |
|||
(The code is written to handle vectors, as the testing part shows) |
|||
<lang octave>function r = F(n) |
|||
for i = 1:length(n) |
|||
if (n(i) == 0) |
|||
r(i) = 1; |
|||
else |
|||
r(i) = n(i) - M(F(n(i)-1)); |
|||
endif |
|||
endfor |
|||
endfunction |
|||
function r = M(n) |
|||
for i = 1:length(n) |
|||
if (n(i) == 0) |
|||
r(i) = 0; |
|||
else |
|||
r(i) = n(i) - F(M(n(i)-1)); |
|||
endif |
|||
endfor |
|||
endfunction</lang> |
|||
<lang octave># testing |
|||
ra = F([0:19]); |
|||
rb = M([0:19]); |
|||
disp(ra); |
|||
disp(rb);</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |