I'm trying to swap first 2 elements in a List using the below function.

  def swap_list(a:List[Int]):List[Int]={
    a match {
      case x::y::Nil => List(y,x)
      case List(x,y,rest @ _*) =>  List(y,x)
      case _ => a
    }
  }

  swap_list(List(10,20,30))

This works. However, If I try to include the rest I'm getting a error like

case List(x,y,rest @ _*) =>  List(y,x) +: rest

Error below

Error:(27, 50) type mismatch;
found   : Seq[Any]
required: List[Int]
case List(x,y,rest @ _*) =>  List(y,x) +: rest

when I'm specifying the function result type in the definition, why am I getting Seq[Any] in the error message?

I need to return List(20,10,30). How to resolve this?.

4 Answers

5
prayagupd On Best Solutions

Apparently operators in scala List are confusing. You need to concat lists using ++,

def swap_list(a:List[Int]):List[Int]={
  a match {
    case x::y::Nil => List(y,x)
    case List(x,y,rest @ _*) =>  List(y,x) ++ rest
    case _ => a
  }
}

val newList = swap_list(List(10, 20, 30))

println(newList) //List(20, 10, 30)

Summary of List operators,

1) prepend on List using +: or ::

scala> 1000 +: List(1, 2, 3)
res1: List[Int] = List(1000, 1, 2, 3)

scala> 1000 :: List(1, 2, 3)
res4: List[Int] = List(1000, 1, 2, 3)

2) append on List using :+

scala> List(1, 2, 3) :+ 100
res2: List[Int] = List(1, 2, 3, 100)

3) concat Lists using ++, same as in haskell

scala> List(1, 2, 3) ++ List(4, 5, 6)
res3: List[Int] = List(1, 2, 3, 4, 5, 6)
3
Gábor Bakos On

You need to ++ instead of +: as the latter is for single element.

5
Luis Miguel Mejía Suárez On

Well, while prayagupd solution works, and clearly explains the problem (and should be the accepted answer IMHO).

I think is worth sharing a "better" solution to this problem, since concatenating lists is expensive, it is better to just prepend elements to them.

def swapList[T](l: List[T]): List[T] = l match {
  case Nil => Nil
  case x :: Nil => x :: Nil
  case x :: y :: xs => y :: x :: xs
}

swapList(List(10,20,30)) // res0: List[Int] = List(20, 10, 30).
3
Tim On

The simplest implementation is this:

def swap_list(a: List[Int]): List[Int] =
  a match {
    case x :: y :: tail => y :: x :: tail
    case _ => a
  }