gonzono
gonzono

Reputation: 13

Not found vale of function

I'm new in Scala and I have a problem. My code:

object testObject {

def last[A](xs:List[A]):A = 
if (xs == Nil) throw new Exception("empty") 
else (xs.reverse).head 

def main(args: Array[String]){
println("Hello, world!")
last(List(1,2,3,4))
}
}

And intepreter says:

<console>:8: error: not found: value last
            last(List(1,2,3,4))
            ^

I clicked "Run selection in scala interpreter" in Eclipse

Upvotes: 0

Views: 92

Answers (2)

Michael Kohl
Michael Kohl

Reputation: 66837

Sorry, I had originally misread your question. If I just copy your code into a Scala REPL, everything seems to be working fine. Have you tried the same?

Some general points though:

  • You don't need (x.reverse).head, x.reverse.head works too.
  • The argument to main is not used in the body, so can be omitted.
  • Your call to last will return the last element of the list but then not use it.

Upvotes: 1

Madoc
Madoc

Reputation: 5919

Works for me:

 Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
 Type in expressions to have them evaluated.
 Type :help for more information.

 scala> object testObject {
      | 
      | def last[A](xs:List[A]):A = 
      | if (xs == Nil) throw new Exception("empty") 
      | else (xs.reverse).head 
      | 
      | def main(args: Array[String]){
      | println("Hello, world!")
      | last(List(1,2,3,4))
      | }
      | }
 defined module testObject

 scala> testObject.main(new Array[String](0))
 Hello, world!

So it must have to do with how you run it. My guess: You clicked "Run Selection" and only had the line with the call to last selected. Try running the testObject type as the main program. (In IntelliJ IDEA, this would be right-clicking inside the editor and selecting "Run 'testObject'". Not sure how it works in Eclipse, but it can't be that different.)

Upvotes: 1

Related Questions