Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE OverloadedStrings, BlockArguments #-}
  2.  
  3. import Data.List
  4. import Data.Ord
  5.  
  6. -- Speed
  7. import qualified Data.ByteString.Char8 as B
  8. import Control.Applicative -- liftA2 is faster than <*> and <$>
  9.  
  10. -- Natural reading direction
  11. import Control.Category ((>>>))
  12. import Data.Function    ((&))
  13.  
  14.  
  15. main = B.interact do                     -- With all input from stdin ..
  16.   res <- B.words                         -- split on whitespace
  17.      >>> sort                            -- sort alphabetically
  18.      >>> group                           -- group on consecutive identical words
  19.      >>> fmap   (liftA2 (,) head length) -- store word and number of occurances
  20.      >>> sortOn (Down . snd)             -- order descendingly by number of occurances
  21.  
  22.   -- Output formatting
  23.   return $ [ wrd <> "\t" <> B.pack (show cnt) | (wrd, cnt) <- res ]
  24.          & B.unlines
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement