Mutual recursion: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) Rename Perl 6 -> Raku, alphabetize, minor clean-up |
|||
Line 243: | Line 243: | ||
end.</lang> |
end.</lang> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 645: | Line 644: | ||
printf("\n"); |
printf("\n"); |
||
return EXIT_SUCCESS; |
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> |
}</lang> |
||
Line 707: | Line 729: | ||
if ( n == 0 ) return 0; |
if ( n == 0 ) return 0; |
||
return n - F(M(n-1)); |
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> |
}</lang> |
||
Line 818: | Line 817: | ||
<pre>[1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12] |
<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> |
[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}}== |
=={{header|Dart}}== |
||
Line 892: | Line 863: | ||
end. |
end. |
||
</lang> |
</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}}== |
=={{header|E}}== |
||
Line 913: | Line 912: | ||
But you don't have to worry about that to use it. |
But you don't have to worry about that to use it. |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<lang Eiffel> |
<lang Eiffel> |
||
Line 968: | Line 968: | ||
0 0 1 2 2 3 4 4 5 6 6 7 7 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> |
</pre> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
{{trans|Smalltalk}} |
{{trans|Smalltalk}} |
||
Line 1,105: | Line 1,106: | ||
} |
} |
||
</lang> |
</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 —i.e. XML, JSON— 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}}== |
=={{header|Forth}}== |
||
Line 1,224: | Line 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 |
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> |
</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 —i.e. XML, JSON— 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}}== |
=={{header|Go}}== |
||
Line 1,856: | Line 1,857: | ||
<pre>1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6 |
<pre>1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6 |
||
0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6</pre> |
0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Line 2,105: | Line 2,105: | ||
echo f(i) |
echo f(i) |
||
echo m(i)</lang> |
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}}== |
=={{header|Objective-C}}== |
||
Line 2,144: | Line 2,169: | ||
return 0; |
return 0; |
||
}</lang> |
}</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}}== |
=={{header|OCaml}}== |
||
Line 2,211: | Line 2,211: | ||
disp(ra); |
disp(ra); |
||
disp(rb);</lang> |
disp(rb);</lang> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 2,343: | Line 2,342: | ||
1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 |
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 |
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> |
</pre> |
||
Line 2,534: | Line 2,519: | ||
> let males = map M (0..10); males; |
> let males = map M (0..10); males; |
||
[0,0,1,2,2,3,4,4,5,6,6]</lang> |
[0,0,1,2,2,3,4,4,5,6,6]</lang> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<lang PureBasic>Declare M(n) |
<lang PureBasic>Declare M(n) |
||
Line 2,601: | Line 2,587: | ||
<lang R>print.table(lapply(0:19, M)) |
<lang R>print.table(lapply(0:19, M)) |
||
print.table(lapply(0:19, F))</lang> |
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}}== |
=={{header|REBOL}}== |
||
Line 2,627: | Line 2,640: | ||
M: [0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12]</pre> |
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}}== |
=={{header|REXX}}== |
||
===vanilla=== |
===vanilla=== |
||
Line 2,846: | Line 2,848: | ||
<pre>1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 |
<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> |
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12</pre> |
||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
Line 3,233: | Line 3,234: | ||
0 OK, 0:199</pre> |
0 OK, 0:199</pre> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Again SHell}} |
{{works with|Bourne Again SHell}} |
||
Line 3,328: | Line 3,330: | ||
<pre> 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13 |
<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> |
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}}== |
=={{header|x86 Assembly}}== |
||
{{works with|nasm}} |
{{works with|nasm}} |