Skip to content

Instantly share code, notes, and snippets.

@blouerat
Created June 2, 2014 09:51
Show Gist options
  • Save blouerat/c960b77a487c8535eda8 to your computer and use it in GitHub Desktop.
Save blouerat/c960b77a487c8535eda8 to your computer and use it in GitHub Desktop.
1HaskellADay: countOccurrences
import Data.Map hiding (foldr)
{-| Count character occurrences in a string
Examples:
>>> lookup 'l' $ countOccurrences "hello"
Just 2
>>> lookup 'n' $ countOccurrences "hello"
Nothing
-}
countOccurrences :: String -> Map Char Int
countOccurrences = foldr f empty
where f c = insertWith (+) c 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment