import qualified Data.ByteString.Lazy.Char8 as S import qualified Data.Map as M import qualified Data.List as L import Data.Function tab = S.pack "\t" showRow wrd ct = S.concat [(S.pack $ show ct), tab, wrd] main = S.getContents >>= \cs -> do let ws = S.words cs counts = L.foldl' (\m w -> M.insertWith' (+) w 1 m) M.empty ws sorted = L.sortBy (flip compare `on` snd) . M.toList $ counts mapM_ (\(word,count) -> S.putStrLn $ showRow word count) sorted