list Pattern Matching to return a new list of every other element

Ben*_*en 2 scala pattern-matching

I need to write a function that takes a List such as ("1","2","3") and return every other element in that list into a new list using pattern matching. What would be the correct case statement to get the head element of the list and then find every other element.

def everyOther[A](list: List[A]): List[A] =
     list match {
     case   _ => Nil
     case x::xs => 
}
Run Code Online (Sandbox Code Playgroud)

It should return a new list of every 2nd element starting from the head element

jwv*_*wvh 5

Recursion to the rescue.

def everyOther[A](list: List[A]): List[A] = list match {
  case Nil => list
  case _ :: Nil => list
  case x :: _ :: xs => x :: everyOther(xs)
}
Run Code Online (Sandbox Code Playgroud)