Created
June 2, 2014 09:51
-
-
Save blouerat/c960b77a487c8535eda8 to your computer and use it in GitHub Desktop.
1HaskellADay: countOccurrences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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