def bar(chars: List[Char]): List[(Char, Int)] = { def foo(cFoo: Char, accFoo: List[(Char, Int)]): List[(Char, Int)] = accFoo match { case Nil => (cFoo, 1) :: accFoo case head :: tail => head match { case (`cFoo`, y) => (cFoo, y + 1) :: tail case (x, y) => (x, y) :: foo(cFoo, tail) } } def blah(cBlah: List[Char], accBlah: List[(Char, Int)]): List[(Char, Int)] = cBlah match { case Nil => accBlah case head :: tail => blah(tail, foo(head, accBlah)) } blah(chars, Nil) }