Alex Rivera | Logout

Accessing the next element in list to compare in Scala

Asked 2011-04-20T07:14:48.840
15

I'm new to Scala and i was wondering how you can call the next element of the list because I am trying to compare the current element with the adjacent one. Given x as the current element, I tried similar to java, x+1 but that didnt work. Any help?

for (x <- list; if (x == (next adj. element))) println("same")
Edit
Report

1 Answer

4
scala> val xs = 1::3::5::4::Nil
xs: List[Int] = List(1, 3, 5, 4)

scala> (xs, xs.tail).zip.foreach(println)
(1,3)
(3,5)
(5,4)

scala>
answered 2011-04-20T11:56:47.737

Your Answer