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.

6
  • seems that X10 treats integers as x10.lang.Long by default, probably need to explicitly cast them to Int whenever necessary?
    – mangusta
    Mar 28, 2014 at 11:09
  • I've tried to replace ever Int by Long - still the same error. My guess was that the call fib may be wrong. Eventually it should be something like this.fib. However, no matter what I did, I always got errors. (this.fib gave me different errors). Mar 28, 2014 at 11:56
  • it doesn't make sense to use keyword this to invoke static function - this refers to class instance and it is the very purpose of static, to be used without class instance :/
    – mangusta
    Mar 28, 2014 at 12:52
  • btw, so, what error do you get when you use Long instead of Int? Expected base type: x10.lang.Long Found base type: x10.lang.Long O_o ? no way
    – mangusta
    Mar 28, 2014 at 12:56
  • it's a year later, how do you like x10?
    – johnbakers
    Jul 8, 2015 at 16:17

1 Answer 1

1

The following version works as expected:

// 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));
        }
    }
}

It seems as if number are Long per standard.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.