I have this code:
class myFunc{
def summatory(list: List[Int]): Int= list match{
case Nil => 0
case _ => list(1) + summatory(list.tail)
}
}
But I'm getting this error: "scalac -classpath . -d . main.scala scala -classpath . Main java.lang.IndexOutOfBoundsException: 1"
How can I solve it?
0rather than1to access the first element. Although, you may rather callheadand avoid that accident again, or even better you use proper pattern matching:case head :: tail => head + summatory(tail)