public
anonymous / string.hs
Created

word length count

  • Download Gist
string.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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'--

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.