def max(xs: List[Int]): Int = {
if (xs.isEmpty) throw new java.util.NoSuchElementException("List is Empty")
else
max1(0,xs)
def max1(num :Int, x : List[Int]) : Int = {
if(x.isEmpty) return num
else if(num>x.head) max1(num,x.tail)
else
max1(x.head,x.tail)
}
}
I am trying to Implement code to throw error when it gets an empty list as input and trying to get the maximum value of the list in a recursive way using another helper function
error: type mismatch; found : Unit required: Int
Put the definition of max1
before the if
statement.
The return value of a function is the result of the last statement in its body. The way you have it now, the last statement is def
, and it's result is Unit
.