Skip to content

Instantly share code, notes, and snippets.

@pgilad pgilad/word_count.hs
Created Dec 16, 2017

What would you like to do?
Dictionary word count in Haskell
import Data.List
import qualified Data.Map.Strict as Map
import Data.Ord (comparing)
wordFrequency :: (Ord a) => [a] -> Map.Map a Int
wordFrequency xs = foldr (\word acc -> Map.insertWith (+) word 1 acc) Map.empty xs
nBiggest :: Int -> Map.Map a Int -> [(a, Int)]
nBiggest n m = take n $ sortBy (comparing $ negate . snd) $ Map.toList m
main :: IO ()
main = do
let list = take 100 $ cycle ["hello", "there", "list", "of", "words", "there", "of", "of"]
let biggest = 2
print $ nBiggest 2 $ wordFrequency list
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.