Mutual recursion: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 243:
 
end.</lang>
 
 
=={{header|AppleScript}}==
Line 645 ⟶ 644:
printf("\n");
return EXIT_SUCCESS;
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>namespace RosettaCode {
class Hofstadter {
static public int F(int n) {
int result = 1;
if (n > 0) {
result = n - M(F(n-1));
}
 
return result;
}
 
static public int M(int n) {
int result = 0;
if (n > 0) {
result = n - F(M(n - 1));
}
 
return result;
}
}
}</lang>
 
Line 707 ⟶ 729:
if ( n == 0 ) return 0;
return n - F(M(n-1));
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>namespace RosettaCode {
class Hofstadter {
static public int F(int n) {
int result = 1;
if (n > 0) {
result = n - M(F(n-1));
}
 
return result;
}
 
static public int M(int n) {
int result = 0;
if (n > 0) {
result = n - F(M(n - 1));
}
 
return result;
}
}
}</lang>
 
Line 818 ⟶ 817:
<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|Déjà Vu}}==
<lang dejavu>F n:
if n:
- n M F -- n
else:
1
 
M n:
if n:
- n F M -- n
else:
0
 
for i range 0 10:
!.( M i F i )</lang>
{{out}}
<pre>0 1
0 1
1 2
2 2
2 3
3 3
4 4
4 5
5 5
6 6
6 6 </pre>
 
=={{header|Dart}}==
Line 892 ⟶ 863:
end.
</lang>
 
=={{header|Déjà Vu}}==
<lang dejavu>F n:
if n:
- n M F -- n
else:
1
 
M n:
if n:
- n F M -- n
else:
0
 
for i range 0 10:
!.( M i F i )</lang>
{{out}}
<pre>0 1
0 1
1 2
2 2
2 3
3 3
4 4
4 5
5 5
6 6
6 6 </pre>
 
=={{header|E}}==
Line 913 ⟶ 912:
 
But you don't have to worry about that to use it.
 
=={{header|Eiffel}}==
<lang Eiffel>
Line 968:
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12
</pre>
 
=={{header|Elena}}==
{{trans|Smalltalk}}
Line 1,105 ⟶ 1,106:
}
</lang>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Mutual_recursion this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
Line 1,224 ⟶ 1,217:
M : 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12 13 14 14 15
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Mutual_recursion this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 1,856 ⟶ 1,857:
<pre>1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6
0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6</pre>
 
 
=={{header|Mathematica}}==
Line 2,105:
echo f(i)
echo m(i)</lang>
 
=={{header|Objeck}}==
{{trans|C}}
 
<lang objeck>
class MutualRecursion {
function : Main(args : String[]) ~ Nil {
for(i := 0; i < 20; i+=1;) {
f(i)->PrintLine();
};
"---"->PrintLine();
for (i := 0; i < 20; i+=1;) {
m(i)->PrintLine();
};
}
function : f(n : Int) ~ Int {
return n = 0 ? 1 : n - m(f(n - 1));
}
function : m(n : Int) ~ Int {
return n = 0 ? 0 : n - f(m(n - 1));
}
}
</lang>
 
=={{header|Objective-C}}==
Line 2,144 ⟶ 2,169:
return 0;
}</lang>
 
=={{header|Objeck}}==
{{trans|C}}
 
<lang objeck>
class MutualRecursion {
function : Main(args : String[]) ~ Nil {
for(i := 0; i < 20; i+=1;) {
f(i)->PrintLine();
};
"---"->PrintLine();
for (i := 0; i < 20; i+=1;) {
m(i)->PrintLine();
};
}
function : f(n : Int) ~ Int {
return n = 0 ? 1 : n - m(f(n - 1));
}
function : m(n : Int) ~ Int {
return n = 0 ? 0 : n - f(m(n - 1));
}
}
</lang>
 
=={{header|OCaml}}==
Line 2,211:
disp(ra);
disp(rb);</lang>
 
 
=={{header|Oforth}}==
Line 2,343 ⟶ 2,342:
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|Perl 6}}==
A direct translation of the definitions of <math>F</math> and <math>M</math>:
<lang perl6>multi F(0) { 1 }; multi M(0) { 0 }
multi F(\𝑛) { 𝑛 - M(F(𝑛 - 1)) }
multi M(\𝑛) { 𝑛 - F(M(𝑛 - 1)) }
 
say map &F, ^20;
say map &M, ^20;</lang>
{{out}}
<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>
 
Line 2,534 ⟶ 2,519:
> let males = map M (0..10); males;
[0,0,1,2,2,3,4,4,5,6,6]</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>Declare M(n)
Line 2,601 ⟶ 2,587:
<lang R>print.table(lapply(0:19, M))
print.table(lapply(0:19, F))</lang>
 
=={{header|Racket}}==
<lang Racket>#lang racket
(define (F n)
(if (>= 0 n)
1
(- n (M (F (sub1 n))))))
 
(define (M n)
(if (>= 0 n)
0
(- n (F (M (sub1 n))))))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
A direct translation of the definitions of <math>F</math> and <math>M</math>:
<lang perl6>multi F(0) { 1 }; multi M(0) { 0 }
multi F(\𝑛) { 𝑛 - M(F(𝑛 - 1)) }
multi M(\𝑛) { 𝑛 - F(M(𝑛 - 1)) }
 
say map &F, ^20;
say map &M, ^20;</lang>
{{out}}
<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|REBOL}}==
Line 2,627 ⟶ 2,640:
M: [0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12]</pre>
 
=={{header|Racket}}==
<lang Racket>#lang racket
(define (F n)
(if (>= 0 n)
1
(- n (M (F (sub1 n))))))
 
(define (M n)
(if (>= 0 n)
0
(- n (F (M (sub1 n))))))</lang>
=={{header|REXX}}==
===vanilla===
Line 2,846 ⟶ 2,848:
<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|S-lang}}==
Line 3,233 ⟶ 3,234:
 
0 OK, 0:199</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Line 3,328 ⟶ 3,330:
<pre> 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12 </pre>
 
=={{header|x86 Assembly}}==
{{works with|nasm}}
10,343

edits