Skip to content

Instantly share code, notes, and snippets.

@pgilad
Created December 16, 2017 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/3420470450e4db1bf6f3a9bef13616c8 to your computer and use it in GitHub Desktop.
Save pgilad/3420470450e4db1bf6f3a9bef13616c8 to your computer and use it in GitHub Desktop.
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