Skip to content

Instantly share code, notes, and snippets.

@markjeee
Created November 12, 2016 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjeee/c2deddfc7eab66635a0e2b1bba304dd3 to your computer and use it in GitHub Desktop.
Save markjeee/c2deddfc7eab66635a0e2b1bba304dd3 to your computer and use it in GitHub Desktop.
module WordCount where
import Data.Char
import Data.List
import Data.Map (fromList)
wordCount s = fromList $ map (\s -> (head s, length s)) $ groupByWords s
normCase :: String -> String
normCase s = [ toLower x | x <- s ]
groupByWords :: String -> [ [ String ] ]
groupByWords s = groupBy (\x y -> x == y) $ sort $ words $ stripPunc $ normCase s
stripPunc :: String -> String
stripPunc s = [ x | x <- s, x `elem` allowedChars ]
allowedChars :: String
allowedChars = ['a'..'z'] ++ ['0'..'9'] ++ " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment