Man or boy test: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 227: Line 227:
*   [[Jensen's Device]]
*   [[Jensen's Device]]
<br>
<br>



=={{header|Ada}}==
=={{header|Ada}}==
Line 664: Line 663:
Output:
Output:
-67
-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++}}==
=={{header|C++}}==
Line 783: Line 833:
std::cout << A(10, L(1), L(-1), L(-1), L(1), L(0)) << std::endl;
std::cout << A(10, L(1), L(-1), L(-1), L(1), L(0)) << std::endl;
return 0;
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>
}</lang>


Line 1,201: Line 1,200:
Writeln(A(10, C(1), C(-1), C(-1), C(1), C(0))); // -67 output
Writeln(A(10, C(1), C(-1), C(-1), C(1), C(0))); // -67 output
end.</lang>
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}}==
=={{header|Dyalect}}==
Line 1,273: Line 1,254:


format-out("%d\n", man-or-boy(10))</lang>
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}}==
=={{header|E}}==
Line 1,315: Line 1,314:
→ stack overflow using FireFox
→ stack overflow using FireFox
</lang>
</lang>



=={{header|Ela}}==
=={{header|Ela}}==
Line 1,521: Line 1,519:
-67
-67
</pre>
</pre>



=={{header|Forth}}==
=={{header|Forth}}==
Line 1,905: Line 1,902:
end</lang>
end</lang>


=={{Header|Io}}==
=={{header|Io}}==


Io is nothing if not aggressively manly.
Io is nothing if not aggressively manly.
Line 2,206: Line 2,203:


echo a(10, () => 1, () => -1, () => -1, () => 1, () => 0)</lang>
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}}==
=={{header|Objective-C}}==
Line 2,336: Line 2,381:
return 0;
return 0;
}</lang>
}</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}}==
=={{header|OCaml}}==
Line 2,443: Line 2,440:
-175416
-175416
</pre>
</pre>



=={{header|Oz}}==
=={{header|Oz}}==
Line 2,516: Line 2,512:
print A(10, map {my $p = \$_; sub{$$p}} 1, -1, -1, 1, 0), "\n";
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}}==
=={{header|Phix}}==
Line 2,814: Line 2,798:
(A 10 (lambda () 1) (lambda () -1) (lambda () -1) (lambda () 1) (lambda () 0))</lang>
(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}}==
=={{header|REXX}}==
Line 2,852: Line 2,849:


puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda {0})</lang>
puts a(10, lambda {1}, lambda {-1}, lambda {-1}, lambda {1}, lambda {0})</lang>



=={{header|Rust}}==
=={{header|Rust}}==
Line 2,953: Line 2,949:
}
}
println(A(10, 1, -1, -1, 1, 0))</lang>
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}}==
=={{header|Sidef}}==
Line 2,978: Line 2,986:
var obj = MOB();
var obj = MOB();
say obj.a(10, ->{1}, ->{-1}, ->{-1}, ->{1}, ->{0});</lang>
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}} ==
=={{header|Smalltalk}} ==
Line 3,004: Line 2,996:
10 x1: [1] x2: [-1] x3: [-1] x4: [1] x5: [0]
10 x1: [1] x2: [-1] x3: [-1] x4: [1] x5: [0]

=={{header|Snap!}}==

[[File:snap-man-or-boy.png]]


=={{header|Sparkling}}==
=={{header|Sparkling}}==
Line 3,280: Line 3,276:
For k=3, result=0
For k=3, result=0
For k=4, result=1
For k=4, result=1



=={{header|TXR}}==
=={{header|TXR}}==