I am trying to solve the following problem from Scala for the impatient. The question is as follows:
Using pattern matching, write a function swap that swaps the first two elements of an array provided its length is at least two.
My solution is:
def swap(sArr:Array[Int]) = sArr.splitAt(2) match {
case (Array(x,y),Array(z)) => Array(y,x,z)
case (Array(x,y),Array()) => Array(y,x)
case _ => sArr
}
My problem is with the first case statement. I think it would pattern-match something like (Array(1,2),Array(3)) whereas I intend it to pattern-match (Array(1,2),Array(3,4,5.....))
Can somebody point out how that would be possible.
Thanks