How to subtract two consecutive element in a list in Scala?
问题 I would like to subtract two consecutive element in a list with numbers in Scala. For example : I have this list : val sortedList = List(4,5,6) I would like to have an output list like diffList =(1, 1) where 5-4 = 1 and 6-5 = 1 . I tried the following code: var sortedList = List[Int]() var diffList = List[Int]() for (i <- 0 to (sortedList.length - 1) ;j <- i + 1 to sortedList.length - 1) { val diff = (sortedList(j) - sortedList(i)) diffList = diffList :+ diff } I have the following result for