Write a Scala program to split a given list into two lists.
object Scala_List { def split_list[A](nums: List[A], n: Int): (List[A], List[A]) = { (nums.take(n), nums.drop(n)) } def main(args: Array[String]): Unit = { val nums1 = List(1,2,3,4,5,6) println("Original List:") println("Split the said list into 2 lists:") println(split_list(nums1, 2)) println(split_list(nums1, 3)) } }
Sample Output:
Original List: Split the said list into 2 lists: (List(1, 2),List(3, 4, 5, 6)) (List(1, 2, 3),List(4, 5, 6))
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer