March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
20
Task/Mutual-recursion/Ada/mutual-recursion-2.ada
Normal file
20
Task/Mutual-recursion/Ada/mutual-recursion-2.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
procedure Mutual_Recursion is
|
||||
function M(N: Natural) return Natural;
|
||||
function F(N: Natural) return Natural;
|
||||
|
||||
function M(N: Natural) return Natural is
|
||||
(if N = 0 then 0 else N – F(M(N–1)));
|
||||
|
||||
function F(N: Natural) return Natural is
|
||||
(if N =0 then 1 else N – M(F(N–1)));
|
||||
begin
|
||||
for I in 0..19 loop
|
||||
Put_Line(Integer'Image(F(I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
for I in 0..19 loop
|
||||
Put_Line(Integer'Image(M(I)));
|
||||
end loop;
|
||||
|
||||
end Mutual_recursion;
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
/*auto*/ int male(in int n) pure nothrow {
|
||||
return n ? (n - female(male(n - 1))) : 0;
|
||||
int male(in int n) pure nothrow {
|
||||
return n ? n - male(n - 1).female : 0;
|
||||
}
|
||||
|
||||
/*auto*/ int female(in int n) pure nothrow {
|
||||
return n ? (n - male(female(n - 1))) : 1;
|
||||
int female(in int n) pure nothrow {
|
||||
return n ? n - female(n - 1).male : 1;
|
||||
}
|
||||
|
||||
void main() {
|
||||
iota(20).map!female().writeln();
|
||||
iota(20).map!male().writeln();
|
||||
20.iota.map!female.writeln;
|
||||
20.iota.map!male.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#import <objc/Object.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface Hofstadter : Object
|
||||
@interface Hofstadter : NSObject
|
||||
+ (int)M: (int)n;
|
||||
+ (int)F: (int)n;
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,7 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
sub F { my $n = shift; $n ? $n - M(F($n-1)) : 1 }
|
||||
sub M { my $n = shift; $n ? $n - F(M($n-1)) : 0 }
|
||||
|
||||
# For mutually recursive functions,
|
||||
# predeclaring is probably a good idea.
|
||||
sub M; sub F;
|
||||
|
||||
sub F { my $n = shift; $n ? $n - M F $n-1 : 1 }
|
||||
sub M { my $n = shift; $n ? $n - F M $n-1 : 0 }
|
||||
|
||||
for my $f (\&F, \&M) {
|
||||
print join(' ', map $f->($_), 0 .. 19), "\n";
|
||||
# Usage:
|
||||
foreach my $sequence (\&F, \&M) {
|
||||
print join(' ', map $sequence->($_), 0 .. 19), "\n";
|
||||
}
|
||||
|
|
|
|||
25
Task/Mutual-recursion/Rust/mutual-recursion.rust
Normal file
25
Task/Mutual-recursion/Rust/mutual-recursion.rust
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
fn f(n: int) -> int {
|
||||
match n {
|
||||
0 => 1,
|
||||
_ => n - m(f(n - 1))
|
||||
}
|
||||
}
|
||||
|
||||
fn m(n: int) -> int {
|
||||
match n {
|
||||
0 => 0,
|
||||
_ => n - f(m(n - 1))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for i in range(0, 20).map(f) {
|
||||
print!("{} ", i);
|
||||
}
|
||||
println!("")
|
||||
|
||||
for i in range(0, 20).map(m) {
|
||||
print!("{} ", i);
|
||||
}
|
||||
println!("")
|
||||
}
|
||||
|
|
@ -1,37 +1,13 @@
|
|||
@(define f (n out))
|
||||
@ (local n1 fn1 mfn1)
|
||||
@ (cases)
|
||||
@ (bind n "0")
|
||||
@ (bind out "1")
|
||||
@ (or)
|
||||
@ (next `!echo $(( @n - 1 ))`)
|
||||
@ n1
|
||||
@ (f n1 fn1)
|
||||
@ (m fn1 mfn1)
|
||||
@ (next `!echo $(( @n - @mfn1 ))`)
|
||||
@ out
|
||||
@ (end)
|
||||
@(end)
|
||||
@(define m (n out))
|
||||
@ (local n1 mn1 fmn1)
|
||||
@ (cases)
|
||||
@ (bind n "0")
|
||||
@ (bind out "0")
|
||||
@ (or)
|
||||
@ (next `!echo $(( @n - 1 ))`)
|
||||
@ n1
|
||||
@ (m n1 mn1)
|
||||
@ (f mn1 fmn1)
|
||||
@ (next `!echo $(( @n - @fmn1 ))`)
|
||||
@ out
|
||||
@ (end)
|
||||
@(end)
|
||||
@(next `!seq 0 15`)
|
||||
@(collect :vars ())
|
||||
@ n
|
||||
@ (f n fn)
|
||||
@ (m n mn)
|
||||
@ (output)
|
||||
f(@n) = @fn; m(@n) = @mn
|
||||
@ (end)
|
||||
@(end)
|
||||
@(do
|
||||
(defun f (n)
|
||||
(if (>= 0 n)
|
||||
1
|
||||
(- n (m (f (- n 1))))))
|
||||
|
||||
(defun m (n)
|
||||
(if (>= 0 n)
|
||||
0
|
||||
(- n (f (m (- n 1))))))
|
||||
|
||||
(each ((n (range 0 15)))
|
||||
(format t "f(~s) = ~s; m(~s) = ~s\n" n (f n) n (m n))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue