Stable identifier required during pattern matching? (Scala)

Viewed 8271

Trying to produce a list of tuples showing prime factor multiplicity... the idea is to match each integer in a sorted list against the first value in a tuple, using the second value to count. Could probably do it more easily with takeWhile, but meh. Unfortunately my solution won't compile:

  def primeFactorMultiplicity (primeFactors: List[Int]) = {

    primeFactors.foldRight (List[(Int, Int)]()) ((a, b) => (a, b) match {
      case (_, Nil)       => (a, 1) :: b
      case (b.head._1, _) => (a, b.head._2 + 1) :: b.tail
      case _              => (a, 1) :: b
    })
  }

It says "error: stable identifier required, but b.head._1 found." But changing the second case line to the following works fine:

      case (i, _) if (i == b.head._1) => (a, b.head._2 + 1) :: b.tail

Why is this, and why can't the compiler cope if there is such a simple fix?

2 Answers
Related