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.
open example.Lists, you will see below lines:
def sum(xs: List[Int]): Int = ???
def max(xs: List[Int]): Int = ???
use 0 instead of ???.
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;
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With