Skip to content

Instantly share code, notes, and snippets.

@larryv
Created June 2, 2014 02:15
Show Gist options
  • Save larryv/77a15a2dae215fa12c17 to your computer and use it in GitHub Desktop.
Save larryv/77a15a2dae215fa12c17 to your computer and use it in GitHub Desktop.
def times(chars: List[Char]): List[(Char, Int)] = {
def incr(pairs: List[(Char, Int)], C: Char): List[(Char, Int)] =
pairs match {
case Nil => List((C, 1))
case (C, n) :: ps => (C, n+1) :: ps
case p :: ps => p :: incr(ps, C)
}
(chars foldLeft List[(Char, Int)]())(incr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment