Jose V
Jose V

Reputation: 1854

if/else in Scala: Illegal start of simple expression/Il

These are the errors:

scala.scala:13: error: not found: value listaFibonaccisAux
  listaFibonaccisAux int 0
  ^
scala.scala:4: error: illegal start of simple expression
  if (int>fibby) fibby :: (listaFibonaccisAux int (n+1))
  ^
scala.scala:1: error: illegal start of definition
  else List()
  ^
scala.scala:1: error: eof expected but '}' found.
  }
  ^

And here's my code, the errors seem to refer to the simple if/else statements, I already tried wrapping and unwrapping stuff in parens, but it did not help:

def listaFibonaccis (int:Int):List[Int]=
  {
  listaFibonaccisAux (int, 0)
  }

def listaFibonaccisAux (int:Int, n:Int):List[Int]=
  {
  var fibby = fib n
  if (int> fibby)
    fibby :: (listaFibonaccisAux (int, (n+1)))
  else 
    List()
  }

def fib( n : Int) : Int = n match 
  {
   case 0 | 1 => n
   case _ => fib( n-1 ) + fib( n-2 )
  }

fib finds a number n in the fibonacci sequence

listaFibonaccisAux creates a list of fibonacci numbers using fib, and stops when the numbers get bigger than int

listaFibonaccis is just a simple wrapper to call the other one with the 0 to start it

it's a plain if/else statement giving me trouble, and that's sad.

Upvotes: 1

Views: 1218

Answers (1)

jwvh
jwvh

Reputation: 51271

The compiler is complaining about the if/else because it doesn't think the line before has ended properly.

Try this: fib(n)

While "infix" (space notation) is sometimes handy...

instance.method(arg) into instance method arg

...it can't be applied as you've tried to use it.

Upvotes: 4

Related Questions