Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
object HelloWorld {
def main(args: Array[String]) {
//I have a List(1 ,2 ,3 ,4 ,5) and trying to get a sublist: List(3, 4) from it by the following way:
val list = List(1 ,2 ,3 ,4 ,5)
//0, 1, 2, 3, 4
//3, 4
var b = list.splitAt(3)._2
println(b)
println(sum(list))
def sum(xs: List[Int]): Int = xs match {
case Nil => 0
case _ :: tail if tail.length==0 => 0 // size 1
case _ :: tail if tail.length==2 => tail(1) //size 2
case _ :: tail if tail.length>2 => tail(0) + tail(1) + sum(tail.splitAt(3)._2)
}
}
}
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more