Skip to content

Instantly share code, notes, and snippets.

@samidarko
Created August 14, 2016 08:33
Show Gist options
  • Save samidarko/27c9abb566e3c5cd2717e1e838e7726e to your computer and use it in GitHub Desktop.
Save samidarko/27c9abb566e3c5cd2717e1e838e7726e to your computer and use it in GitHub Desktop.
Count the number of occurrences in a string over consumption
import qualified Data.Char as Char
countOccurrences :: String -> String
countOccurrences "" = ""
countOccurrences s = let (fw, rest) = span (\x -> x == head s) s in (Char.intToDigit $ length $ fw) : (head s) : (countOccurrences rest)
countOccurrences "aaaaabbbbccccccaaaaaaa"
-- "5a4b6c7a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment