Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2013 15:37
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 anonymous/5850953 to your computer and use it in GitHub Desktop.
Save anonymous/5850953 to your computer and use it in GitHub Desktop.
word length count
import System.Environment (getArgs)
import Data.List (sortBy,sort, group)
import Data.Ord (comparing)
import Data.Char (toLower, isLetter)
import qualified Data.HashMap.Strict as HM
createReport :: Int -> String -> String
createReport n =
unlines
. map (\(w,count) -> w ++ " " ++ show count)
. take n
. sortBy (flip $ comparing snd)
. HM.toList
. HM.fromListWith (+)
. map (\w -> (w, 1))
. words
. map rnl
main = getArgs >>= \[nstr] -> interact (createReport (read nstr))
-- unicode avoidance:
rnl :: Char -> Char
rnl = \c -> case c of
'a' -> 'a'
'b' -> 'b'
'c' -> 'c'
'd' -> 'd'
'e' -> 'e'
'f' -> 'f'
'g' -> 'g'
'h' -> 'h'
'i' -> 'i'
'j' -> 'j'
'k' -> 'k'
'l' -> 'l'
'm' -> 'm'
'n' -> 'n'
'o' -> 'o'
'p' -> 'p'
'q' -> 'q'
'r' -> 'r'
's' -> 's'
't' -> 't'
'u' -> 'u'
'v' -> 'v'
'w' -> 'w'
'x' -> 'x'
'y' -> 'y'
'z' -> 'z'
'A' -> 'a'
'B' -> 'b'
'C' -> 'c'
'D' -> 'd'
'E' -> 'e'
'F' -> 'f'
'G' -> 'g'
'H' -> 'h'
'I' -> 'i'
'J' -> 'j'
'K' -> 'k'
'L' -> 'l'
'M' -> 'm'
'N' -> 'n'
'O' -> 'o'
'P' -> 'p'
'Q' -> 'q'
'R' -> 'r'
'S' -> 's'
'T' -> 't'
'U' -> 'u'
'V' -> 'v'
'W' -> 'w'
'X' -> 'x'
'Y' -> 'y'
'Z' -> 'z'
_ -> '\n'--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment