Search code examples
scalapattern-matchingunreachable-code

Unreachable code in scala?


For some reason the following code is unreachable. I cannot understand why my code will never get reached as this is a simple pattern matching. Here it is:

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)
}

This algorithm is meant to extract all of the sub lists of the given list.


Solution

  • case x => List(x) matches anything. It looks like you want to match a 1-element list so you can use:

    case l@List(_) => List(l)