Jump to content

Man or boy test: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 227:
*   [[Jensen's Device]]
<br>
 
 
=={{header|Ada}}==
Line 664 ⟶ 663:
Output:
-67
 
=={{header|C sharp|C#}}==
C# 2.0 supports anonymous methods which are used in the implementation below:
 
{{works with|C sharp|C#|2+}}
 
<lang csharp>using System;
delegate T Func<T>();
class ManOrBoy
{
static void Main()
{
Console.WriteLine(A(10, C(1), C(-1), C(-1), C(1), C(0)));
}
static Func<int> C(int i)
{
return delegate { return i; };
}
static int A(int k, Func<int> x1, Func<int> x2, Func<int> x3, Func<int> x4, Func<int> x5)
{
Func<int> b = null;
b = delegate { k--; return A(k, b, x1, x2, x3, x4); };
return k <= 0 ? x4() + x5() : b();
}
}
</lang>
 
C# 3.0 supports lambda expressions which are used in the implementation below:
 
{{works with|C sharp|C#|3+}}
 
<lang csharp>using System;
class ManOrBoy
{
static void Main()
{
Console.WriteLine(A(10, () => 1, () => -1, () => -1, () => 1, () => 0));
}
static int A(int k, Func<int> x1, Func<int> x2, Func<int> x3, Func<int> x4, Func<int> x5)
{
Func<int> b = null;
b = () => { k--; return A(k, b, x1, x2, x3, x4); };
return k <= 0 ? x4() + x5() : b();
}
}</lang>
 
=={{header|C++}}==
Line 783 ⟶ 833:
std::cout << A(10, L(1), L(-1), L(-1), L(1), L(0)) << std::endl;
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
C# 2.0 supports anonymous methods which are used in the implementation below:
 
{{works with|C sharp|C#|2+}}
 
<lang csharp>using System;
delegate T Func<T>();
class ManOrBoy
{
static void Main()
{
Console.WriteLine(A(10, C(1), C(-1), C(-1), C(1), C(0)));
}
static Func<int> C(int i)
{
return delegate { return i; };
}
static int A(int k, Func<int> x1, Func<int> x2, Func<int> x3, Func<int> x4, Func<int> x5)
{
Func<int> b = null;
b = delegate { k--; return A(k, b, x1, x2, x3, x4); };
return k <= 0 ? x4() + x5() : b();
}
}
</lang>
 
C# 3.0 supports lambda expressions which are used in the implementation below:
 
{{works with|C sharp|C#|3+}}
 
<lang csharp>using System;
class ManOrBoy
{
static void Main()
{
Console.WriteLine(A(10, () => 1, () => -1, () => -1, () => 1, () => 0));
}
static int A(int k, Func<int> x1, Func<int> x2, Func<int> x3, Func<int> x4, Func<int> x5)
{
Func<int> b = null;
b = () => { k--; return A(k, b, x1, x2, x3, x4); };
return k <= 0 ? x4() + x5() : b();
}
}</lang>
 
Line 1,201 ⟶ 1,200:
Writeln(A(10, C(1), C(-1), C(-1), C(1), C(0))); // -67 output
end.</lang>
 
=={{header|Déjà Vu}}==
 
{{trans|Python}}
 
<lang dejavu>a k x1 x2 x3 x4 x5:
local b:
set :k -- k
a k @b @x1 @x2 @x3 @x4
if <= k 0:
+ x4 x5
else:
b
local x i:
labda:
i
 
!. a 10 x 1 x -1 x -1 x 1 x 0</lang>
 
=={{header|Dyalect}}==
Line 1,273 ⟶ 1,254:
 
format-out("%d\n", man-or-boy(10))</lang>
 
=={{header|Déjà Vu}}==
 
{{trans|Python}}
 
<lang dejavu>a k x1 x2 x3 x4 x5:
local b:
set :k -- k
a k @b @x1 @x2 @x3 @x4
if <= k 0:
+ x4 x5
else:
b
local x i:
labda:
i
 
!. a 10 x 1 x -1 x -1 x 1 x 0</lang>
 
=={{header|E}}==
Line 1,315 ⟶ 1,314:
→ stack overflow using FireFox
</lang>
 
 
=={{header|Ela}}==
Line 1,521 ⟶ 1,519:
-67
</pre>
 
 
=={{header|Forth}}==
Line 1,905 ⟶ 1,902:
end</lang>
 
=={{Headerheader|Io}}==
 
Io is nothing if not aggressively manly.
Line 2,206 ⟶ 2,203:
 
echo a(10, () => 1, () => -1, () => -1, () => 1, () => 0)</lang>
 
=={{header|Objeck}}==
Using anonymous classes instead of closures
<lang objeck>interface Arg {
method : virtual : public : Run() ~ Int;
}
 
class ManOrBoy {
New() {}
function : A(mb : ManOrBoy, k : Int, x1 : Arg, x2 : Arg, x3 : Arg, x4 : Arg, x5 : Arg) ~ Int {
if(k <= 0) {
return x4->Run() + x5->Run();
};
return Base->New(mb, k, x1, x2, x3, x4) implements Arg {
@mb : ManOrBoy; @k : Int; @x1 : Arg; @x2 : Arg; @x3 : Arg; @x4 : Arg; @m : Int;
New(mb : ManOrBoy, k : Int, x1 : Arg, x2 : Arg, x3 : Arg, x4 : Arg) {
@mb := mb; @k := k; @x1 := x1; @x2 := x2; @x3 := x3; @x4 := x4; @m := @k;
}
method : public : Run() ~ Int {
@m -= 1;
return @mb->A(@mb, @m, @self, @x1, @x2, @x3, @x4);
}
}->Run();
}
function : C(i : Int) ~ Arg {
return Base->New(i) implements Arg {
@i : Int;
New(i : Int) {
@i := i;
}
method : public : Run() ~ Int {
return @i;
}
};
}
function : Main(args : String[]) ~ Nil {
mb := ManOrBoy->New();
mb->A(mb, 10, C(1), C(-1), C(-1), C(1), C(0))->PrintLine();
}
}
</lang>
 
=={{header|Objective-C}}==
Line 2,336 ⟶ 2,381:
return 0;
}</lang>
 
=={{header|Objeck}}==
Using anonymous classes instead of closures
<lang objeck>interface Arg {
method : virtual : public : Run() ~ Int;
}
 
class ManOrBoy {
New() {}
function : A(mb : ManOrBoy, k : Int, x1 : Arg, x2 : Arg, x3 : Arg, x4 : Arg, x5 : Arg) ~ Int {
if(k <= 0) {
return x4->Run() + x5->Run();
};
return Base->New(mb, k, x1, x2, x3, x4) implements Arg {
@mb : ManOrBoy; @k : Int; @x1 : Arg; @x2 : Arg; @x3 : Arg; @x4 : Arg; @m : Int;
New(mb : ManOrBoy, k : Int, x1 : Arg, x2 : Arg, x3 : Arg, x4 : Arg) {
@mb := mb; @k := k; @x1 := x1; @x2 := x2; @x3 := x3; @x4 := x4; @m := @k;
}
method : public : Run() ~ Int {
@m -= 1;
return @mb->A(@mb, @m, @self, @x1, @x2, @x3, @x4);
}
}->Run();
}
function : C(i : Int) ~ Arg {
return Base->New(i) implements Arg {
@i : Int;
New(i : Int) {
@i := i;
}
method : public : Run() ~ Int {
return @i;
}
};
}
function : Main(args : String[]) ~ Nil {
mb := ManOrBoy->New();
mb->A(mb, 10, C(1), C(-1), C(-1), C(1), C(0))->PrintLine();
}
}
</lang>
 
=={{header|OCaml}}==
Line 2,443 ⟶ 2,440:
-175416
</pre>
 
 
=={{header|Oz}}==
Line 2,516 ⟶ 2,512:
print A(10, map {my $p = \$_; sub{$$p}} 1, -1, -1, 1, 0), "\n";
-->
 
=={{header|Perl 6}}==
This solution avoids creating the closure B if $k <= 0 (that is, nearly every time).
<lang perl6>sub A($k is copy, &x1, &x2, &x3, &x4, &x5) {
$k <= 0
?? x4() + x5()
!! (my &B = { A(--$k, &B, &x1, &x2, &x3, &x4) })();
};
say A(10, {1}, {-1}, {-1}, {1}, {0});</lang>
{{out}}
<pre>-67</pre>
 
=={{header|Phix}}==
Line 2,814 ⟶ 2,798:
(A 10 (lambda () 1) (lambda () -1) (lambda () -1) (lambda () 1) (lambda () 0))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
This solution avoids creating the closure B if $k <= 0 (that is, nearly every time).
<lang perl6>sub A($k is copy, &x1, &x2, &x3, &x4, &x5) {
$k <= 0
?? x4() + x5()
!! (my &B = { A(--$k, &B, &x1, &x2, &x3, &x4) })();
};
say A(10, {1}, {-1}, {-1}, {1}, {0});</lang>
{{out}}
<pre>-67</pre>
 
=={{header|REXX}}==
Line 2,852 ⟶ 2,849:
 
puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda {0})</lang>
 
 
=={{header|Rust}}==
Line 2,953 ⟶ 2,949:
}
println(A(10, 1, -1, -1, 1, 0))</lang>
 
=={{header|Scheme}}==
 
<lang scheme>(define (A k x1 x2 x3 x4 x5)
(define (B)
(set! k (- k 1))
(A k B x1 x2 x3 x4))
(if (<= k 0)
(+ (x4) (x5))
(B)))
 
(A 10 (lambda () 1) (lambda () -1) (lambda () -1) (lambda () 1) (lambda () 0))</lang>
 
=={{header|Sidef}}==
Line 2,978 ⟶ 2,986:
var obj = MOB();
say obj.a(10, ->{1}, ->{-1}, ->{-1}, ->{1}, ->{0});</lang>
 
=={{header|Snap!}}==
 
[[File:snap-man-or-boy.png]]
 
=={{header|Scheme}}==
 
<lang scheme>(define (A k x1 x2 x3 x4 x5)
(define (B)
(set! k (- k 1))
(A k B x1 x2 x3 x4))
(if (<= k 0)
(+ (x4) (x5))
(B)))
 
(A 10 (lambda () 1) (lambda () -1) (lambda () -1) (lambda () 1) (lambda () 0))</lang>
 
=={{header|Smalltalk}} ==
Line 3,004 ⟶ 2,996:
10 x1: [1] x2: [-1] x3: [-1] x4: [1] x5: [0]
 
=={{header|Snap!}}==
 
[[File:snap-man-or-boy.png]]
 
=={{header|Sparkling}}==
Line 3,280 ⟶ 3,276:
For k=3, result=0
For k=4, result=1
 
 
=={{header|TXR}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.