Created
December 16, 2017 20:28
-
-
Save pgilad/3420470450e4db1bf6f3a9bef13616c8 to your computer and use it in GitHub Desktop.
Dictionary word count in Haskell
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.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