小编Ben*_*en 的帖子

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

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

scala pattern-matching

2
推荐指数
1
解决办法
64
查看次数

标签 统计

pattern-matching ×1

scala ×1