1

I would like to generate and print the first 10 Fibonacci numbers. I don't want to be efficient, but I want to see some (working) X10 code that is easy to understand.

My try

// file Fibonacci.x10
public class Fibonacci {
    public static def fib(n:Int): Int {
        if (n < 2) {
            return n;
        }

        val f1:Int;
        val f2:Int;
        finish {
            async f1 = fib(n-1);
            async f2 = fib(n-2);
        }
        return f1 + f2;
    }

    public static def main(args:Rail[String]) {
        x10.io.Console.OUT.println("This is fibonacci in X10.");
        for (var i:Int=0; i < 10; ++i) {
            x10.io.Console.OUT.println(i + ": " + fib(i));
            fib(i);
        }
    }
}

When I compile this, I get:

/home/moose/Fibonacci.x10:11: No valid method call found for call in given type.    
          Call: fib(x10.lang.Long)    
          Type: Fibonacci
/home/moose/Fibonacci.x10:12: No valid method call found for call in given type.    
          Call: fib(x10.lang.Long)    
          Type: Fibonacci
/home/moose/Fibonacci.x10:19: Cannot assign expression to target; base types are incompatible.    
          Expression: 0L    
          Expected base type: x10.lang.Int    
          Found base type: x10.lang.Long
3 errors.

I use X10 release 2.4.2.

4

1 回答 1

1

以下版本按预期工作:

// file Fibonacci.x10
public class Fibonacci {
    public static def fib(n:Long): Long {
        if (n < 2) {
            return n;
        }

        val f1:Long;
        val f2:Long;
        finish {
            async f1 = fib(n-1);
            async f2 = fib(n-2);
        }
        return f1 + f2;
    }

    public static def main(args:Rail[String]) {
        x10.io.Console.OUT.println("This is fibonacci in X10.");
        for (var i:Long=0; i < 10; ++i) {
            x10.io.Console.OUT.println(i + ": " + fib(i));
        }
    }
}

似乎数字是Long符合标准的。

于 2014-03-28T15:32:13.857 回答