Skip to content

Instantly share code, notes, and snippets.

@josephok
Created April 6, 2016 10:09
Show Gist options
  • Save josephok/f8f06e1e117c5c8229ea2cd4c6ef4dfb to your computer and use it in GitHub Desktop.
Save josephok/f8f06e1e117c5c8229ea2cd4c6ef4dfb to your computer and use it in GitHub Desktop.
scala for the impatient excercises chapter 3 excercise 2
def swap_array(a: Array[Int]) = {
for (i <- 0 until a.length) {
if ((i % 2 == 0) && (i+1) < a.length) {
val t = a(i)
a(i) = a(i+1)
a(i+1) = t
}
}
a
}
val a = Array(1,2,3,4,5,6)
println(a.toList)
println(swap_array(a).toList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment