Mutual recursion: Difference between revisions

Add ABC
(→‎{{header|CLU}}: Correct and clarify the Clu example. No special tricks are needed for mutual recursion, it's standard practice to "spec" the clu file before compiling it.)
(Add ABC)
 
(15 intermediate revisions by 11 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,049 ⟶ 1,092:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% To declare things you can either write an .spc file or you can use
% To declare things you can either write an .spc file or you can use
% the clu file itself as a specfile. For a small program a common
% idiom is to spec and compile the same source file:
Line 1,065 ⟶ 1,109:
stream$puts(po, name || ":")
for i: int in int$from_to(0, 15) do
stream$puts(po, " " || int$unparse(fn(i)))
end
stream$putl(po, "")
Line 1,072 ⟶ 1,116:
F = proc (n: int) returns (int)
if n = 0 then
return (1)
else
return (n - M(F(n-1)))
end
end F
Line 1,080 ⟶ 1,124:
M = proc (n: int) returns (int)
if n = 0 then
return (0)
else
return (n - F(M(n-1)))
end
end M
Line 1,277 ⟶ 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,336 ⟶ 1,404:
=={{header|Elena}}==
{{trans|Smalltalk}}
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'collections;
Line 1,348 ⟶ 1,416:
var rb := new ArrayList();
for(int i := 0,; i <= 19,; i += 1)
{
ra.append(F(i));
Line 1,612 ⟶ 1,680:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Mutual_recursion}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Mutual recursion 01.png]]
In '''[https://formulae.org/?example=Mutual_recursion this]''' page you can see the program(s) related to this task and their results.
 
[[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,928 ⟶ 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 2,737 ⟶ 2,864:
echo f(i)
echo m(i)</syntaxhighlight>
 
=={{header|Oberon-2}}==
{{trans|Modula-2}}
<syntaxhighlight lang="oberon2">
MODULE MutualRecursion;
 
IMPORT Out;
 
TYPE
Fn = PROCEDURE(n:INTEGER):INTEGER;
PROCEDURE^ M(n:INTEGER):INTEGER;
PROCEDURE F(n:INTEGER):INTEGER;
BEGIN
IF n=0 THEN RETURN 1
ELSE RETURN n-M(F(n-1))
END;
END F;
 
PROCEDURE M(n:INTEGER):INTEGER;
BEGIN
IF n=0 THEN RETURN 0
ELSE RETURN n-F(M(n-1))
END;
END M;
 
(* Print the first few values of one of the functions *)
PROCEDURE Show(name:ARRAY OF CHAR;fn:Fn);
CONST Max = 15;
VAR i:INTEGER;
BEGIN
Out.String(name);
Out.String(": ");
FOR i := 0 TO Max DO
Out.Int(fn(i),0);
Out.String(" ");
END;
Out.Ln;
END Show;
 
(* Show the first values of both F and M *)
BEGIN
Show("F", F);
Show("M", M);
END MutualRecursion.
</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|Objeck}}==
Line 3,365 ⟶ 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,506 ⟶ 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,715 ⟶ 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,195 ⟶ 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,095

edits