Bul*_*ula 1 scala pattern-matching unreachable-code
由于某种原因,以下代码无法访问.我无法理解为什么我的代码永远不会到达,因为这是一个简单的模式匹配.这里是:
type Occurrences = List[(Char, Int)]
def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match{
case Nil => Nil
case List() => List()
case x => List(x)
case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
}
Run Code Online (Sandbox Code Playgroud)
该算法旨在提取给定列表的所有子列表.
case x => List(x)匹配任何东西 看起来您想要匹配1个元素的列表,以便您可以使用:
case l@List(_) => List(l)
Run Code Online (Sandbox Code Playgroud)