Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala.NotImplementedError: an implementation is missing?

Tags:

scala

Here is my code:

package example

object Lists {

  def max(xs: List[Int]): Int = {
    if(xs.isEmpty){
        throw new java.util.NoSuchElementException()
    }
    else {
        max(xs.tail)
    }
  }
}

When i run it in sbt console:

scala> import example.Lists._
scala> max(List(1,3,2))

I have the following error:

Scala.NotImplementedError: an implementation is missing

How can I fix that?

Thanks.

like image 763
Michael Avatar asked Sep 19 '13 18:09

Michael


2 Answers

open example.Lists, you will see below lines:

def sum(xs: List[Int]): Int = ???
def max(xs: List[Int]): Int = ???

use 0 instead of ???.

like image 179
Shen Qi Avatar answered Sep 24 '22 14:09

Shen Qi


Also, putting the correct recursive implementation for the max that should work

  def max(xs: List[Int]): Int = {
    if(xs.isEmpty){
      throw new java.util.NoSuchElementException()
    }
    val tailMax =  if (xs.tail.isEmpty)  xs.head else max(xs.tail)
    if (xs.head >= tailMax){
      xs.head
    }
    else  tailMax;
  }
like image 40
rajnish Avatar answered Sep 21 '22 14:09

rajnish