Mutual recursion: Difference between revisions

Add ABC
(Add ABC)
 
(7 intermediate revisions by 6 users not shown)
Line 170:
m(0 - 19): 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN f n:
IF n=0: RETURN 1
RETURN n - m f (n-1)
 
HOW TO RETURN m n:
IF n=0: RETURN 0
RETURN n - f m (n-1)
 
WRITE "F:"
FOR n IN {0..15}: WRITE f n
WRITE /
 
WRITE "M:"
FOR n IN {0..15}: WRITE m n
WRITE /</syntaxhighlight>
{{out}}
<pre>F: 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9
M: 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9</pre>
 
=={{header|ACL2}}==
Line 890 ⟶ 910:
p 0.to(20).map! { n | female n }
p 0.to(20).map! { n | male n }</syntaxhighlight>
 
=={{header|Bruijn}}==
Normally it's not possible to call functions before they are defined. We can still induce mutual recursion using its [[Variadic_fixed-point_combinator|variadic fixed-point combinator]].
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/Number .
:import std/List .
 
f' [[[=?0 (+1) (0 - (1 (2 --0)))]]]
 
m' [[[=?0 (+0) (0 - (2 (1 --0)))]]]
 
f ^(y* (f' : {}m'))
 
m _(y* (f' : {}m'))
 
:test ((f (+0)) =? (+1)) ([[1]])
:test ((m (+0)) =? (+0)) ([[1]])
:test ((f (+4)) =? (+3)) ([[1]])
:test ((m (+4)) =? (+2)) ([[1]])
:test ((f (+15)) =? (+9)) ([[1]])
:test ((m (+15)) =? (+9)) ([[1]])
</syntaxhighlight>
 
=={{header|C}}==
Line 1,278 ⟶ 1,321:
 
But you don't have to worry about that to use it.
 
=={{header|EasyLang}}==
<syntaxhighlight>
funcdecl M n .
func F n .
if n = 0
return 1
.
return n - M F (n - 1)
.
func M n .
if n = 0
return 0
.
return n - F M (n - 1)
.
for i = 0 to 15
write F i & " "
.
print ""
for i = 0 to 15
write M i & " "
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,337 ⟶ 1,404:
=={{header|Elena}}==
{{trans|Smalltalk}}
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'collections;
Line 1,349 ⟶ 1,416:
var rb := new ArrayList();
for(int i := 0,; i <= 19,; i += 1)
{
ra.append(F(i));
Line 1,988 ⟶ 2,055:
for (i in 0..n) print("%3d".format(i))
println()
println("-".repeat(78(n + 2) * 3))
print("F :")
for (i in 0..24n) print("%3d".format(f(i)))
println()
print("M :")
for (i in 0..24n) print("%3d".format(m(i)))
println()
}</syntaxhighlight>
Line 3,475 ⟶ 3,542:
<pre>F: [1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12]
M: [0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12]</pre>
 
=={{header|REFAL}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout 'F: ' <S F 0 14>>
<Prout 'M: ' <S M 0 14>>;
};
 
 
F { 0 = 1; s.N = <- s.N <M <F <- s.N 1>>>>; };
M { 0 = 0; s.N = <- s.N <F <M <- s.N 1>>>>; };
 
S {
s.F s.N s.M, <Compare s.N s.M>: '+' = ;
s.F s.N s.M = <Mu s.F s.N> <S s.F <+ s.N 1> s.M>;
};</syntaxhighlight>
{{out}}
<pre>F: 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9
M: 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9</pre>
 
=={{header|REXX}}==
Line 3,842 ⟶ 3,927:
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program mutual_recursion;
print("F", [f(n) : n in [0..14]]);
print("M", [m(n) : n in [0..14]]);
 
proc f(n);
return {[0,1]}(n) ? n - m(f(n-1));
end proc;
 
proc m(n);
return {[0,0]}(n) ? n - f(m(n-1));
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>F [1 1 2 2 3 3 4 5 5 6 6 7 8 8 9]
M [0 0 1 2 2 3 4 4 5 6 6 7 7 8 9]</pre>
 
=={{header|Sidef}}==
Line 4,322 ⟶ 4,424:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var MF = //Fn.new forward{ declaration|n|
 
var F = Fn.new { |n|
if (n == 0) return 1
return n - M.call(F.call(n-1))
}
 
var M = Fn.new { |n|
if (n == 0) return 0
return n - F.call(M.call(n-1))
2,093

edits