| 1 |
// TAKL -- The TAKeuchi function using lists as counters. |
| 2 |
|
| 3 |
class Pair { |
| 4 |
|
| 5 |
// A Java compiler ought to generate inline code for these. |
| 6 |
|
| 7 |
public static Pair cons (int n, Pair y) { return new Pair(n, y); } |
| 8 |
public static int car (Pair x) { return x.hd; } |
| 9 |
public static Pair cdr (Pair x) { return x.tl; } |
| 10 |
|
| 11 |
// If it doesn't, then we'll inline them by hand. |
| 12 |
// That's why the following are public. |
| 13 |
|
| 14 |
public Pair (int n, Pair y) { hd = n; tl = y; } |
| 15 |
public int hd; |
| 16 |
public Pair tl; |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
class temp { |
| 21 |
|
| 22 |
static Pair listn (int n) { |
| 23 |
if (n != 0) |
| 24 |
return new Pair (n, listn (n - 1)); |
| 25 |
else |
| 26 |
return null; |
| 27 |
} |
| 28 |
|
| 29 |
// The boolean expression below comes from the original TAKL |
| 30 |
// benchmark, and remains because it is fun to see which compilers |
| 31 |
// can generate good code from it. See NTAKL for a version in |
| 32 |
// which this mess has been cleaned up. |
| 33 |
|
| 34 |
static boolean shorterp (Pair x, Pair y) { |
| 35 |
return (y != null) && |
| 36 |
((x == null) || |
| 37 |
shorterp (x.tl, y.tl)); |
| 38 |
} |
| 39 |
|
| 40 |
static Pair mas (Pair x, Pair y, Pair z) { |
| 41 |
if (! shorterp (y, x)) |
| 42 |
return z; |
| 43 |
else |
| 44 |
return mas( mas( x.tl, y, z ), |
| 45 |
mas( y.tl, z, x ), |
| 46 |
mas( z.tl, x, y ) ); |
| 47 |
} |
| 48 |
|
| 49 |
static Pair l18 = listn(18); |
| 50 |
static Pair l12 = listn(12); |
| 51 |
static Pair l6 = listn(6); |
| 52 |
|
| 53 |
static int result; |
| 54 |
|
| 55 |
static void test_takl() { |
| 56 |
result = Pair.car( mas( l18, l12, l6 ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/*===========================================================================*/ |
| 60 |
|
| 61 |
public static void main (String args[]) |
| 62 |
{ |
| 63 |
int i; |
| 64 |
|
| 65 |
for (i=0; i<200; i++) |
| 66 |
test_takl(); |
| 67 |
|
| 68 |
if (result != 7) |
| 69 |
System.out.println ("*** wrong result ***"); |
| 70 |
} |
| 71 |
|
| 72 |
} |