Mutual recursion: Difference between revisions
Add ABC
Deadmarshal (talk | contribs) m (Added Oberon-2 output) |
Not a robot (talk | contribs) (Add ABC) |
||
(12 intermediate revisions by 9 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
<syntaxhighlight lang="elena">import extensions;
import system'collections;
Line 1,349 ⟶ 1,416:
var rb := new ArrayList();
for(int i := 0
{
ra.append(F(i));
Line 1,613 ⟶ 1,680:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Mutual_recursion}}
'''Solution'''
[[File:Fōrmulæ - Mutual recursion 01.png]]
[[File:Fōrmulæ - Mutual recursion 02.png]]
[[File:Fōrmulæ - Mutual recursion 03.png]]
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
def fn F( n as long ) as long
def fn M( n as long ) as long
local fn F( n as long ) as long
long result
if n == 0 then exit fn = 1
result = n - fn M( fn F( n-1 ) )
end fn = result
local fn M( n as long ) as long
long result
if n == 0 then exit fn = 0
result = n - fn F( fn M( n-1 ) )
end fn = result
long i, counter
counter = 0
for i = 0 to 19
printf @"%3ld\b", fn F( i )
counter++
if counter mod 5 == 0 then print : counter = 0
next
print : print
counter = 0
for i = 0 to 19
printf @"%3ld\b", fn M( i )
counter++
if counter mod 5 == 0 then print : counter = 0
next
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1 1 2 2 3
3 4 5 5 6
6 7 8 8 9
9 10 11 11 12
0 0 1 2 2
3 4 4 5 6
6 7 7 8 9
9 10 11 11 12
</pre>
=={{header|Go}}==
Line 1,929 ⟶ 2,055:
for (i in 0..n) print("%3d".format(i))
println()
println("-".repeat(
print("F :")
for (i in 0..
println()
print("M :")
for (i in 0..
println()
}</syntaxhighlight>
Line 3,416 ⟶ 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,557 ⟶ 3,701:
return mr
</syntaxhighlight>
=={{header|RPL}}==
≪ IF DUP THEN DUP 1 - '''FEML MALE''' - ELSE DROP 1 END
≫ ''''FEML'''' STO ( n -- F(n) )
For M(n), here is a little variant, less readable but saving one word !
≪ IF THEN LAST DUP 1 - '''MALE FEML''' - ELSE 0 END
≫ ''''MALE'''' STO ( n -- M(n) )
{{in}}
<pre>
≪ {} 0 20 FOR n n MALE + NEXT ≫ EVAL
≪ {} 0 20 FOR n n FEML + NEXT ≫ EVAL
</pre>
{{out}}
<pre>
2: { 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12 }
1: { 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13 }
</pre>
=={{header|Ruby}}==
Line 3,766 ⟶ 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,246 ⟶ 4,424:
=={{header|Wren}}==
<syntaxhighlight lang="
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))
|