Complete beginner to Scala and just trying to figure out the basics right now.
As part of a tutorial I'm trying to create a function that returns the largest element in a list of integers. To accomplish this I've (tentatively) put together a the following code:
def max(xs: List[Int]): Int = if (xs.isEmpty) throw new java.util.NoSuchElementException else findMax(xs.head, xs.tail) def findMax(a: Int, b: List[Int]) { if (b.isEmpty) return a if (a > b.head) findMax(a, b.tail) else findMax(b.head, b.tail) } However, when I try to compile it I get a type error for line 5.
[error] /scala/example/src/main/scala/example/Lists.scala:5: type mismatch; [error] found : Unit [error] required: Int [error] findMax(xs.head, xs.tail) I have to admit I'm a bit confused by this error message as I don't understand how the compiler thinks I'm trying to pass in a Unit type given the logic to ensure a List is not empty prior to this line.
Can anyone help to clarify the issue here?