As it is asking for the type annotation, in Scala the recursive function are not able to infer the return type of the function so. That’s something we need to do in your case it will be the Int.
Just type annotate the method like this.
val sumRecursive :Int= (list: List[Int]) => list match {
case Nil => 0
case x::xs => x + sumRecursive(xs)}
How Int I think you want to know that.
Suppose you have 3 elements in the list: 1,2,3
sunRecursive(list): Int
It will go into the case x::xs which means
x is the head of the list and xs is the tail.
1st step
So you do 1 + sumRecursive(xs) //xs=2, 3
2nd step 2+sumResursive(xs) //xs=3
3rd step 3 +sumResursive(xs) //xs=Nil
It will go into the first case and return 0.
So 3rd step willreturn 3+0 to the second step
It will become2+3 and return to the 1st step
It will become1+2+3 which is 6 and it will return 6
So the return type will be Int ultimately.
def sumRecursive(list: List[Int]), without the=character between name and parameter. - riccardo.cardin