Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings, BlockArguments #-}
- import Data.List
- import Data.Ord
- -- Speed
- import qualified Data.ByteString.Char8 as B
- import Control.Applicative -- liftA2 is faster than <*> and <$>
- -- Natural reading direction
- import Control.Category ((>>>))
- import Data.Function ((&))
- main = B.interact do -- With all input from stdin ..
- res <- B.words -- split on whitespace
- >>> sort -- sort alphabetically
- >>> group -- group on consecutive identical words
- >>> fmap (liftA2 (,) head length) -- store word and number of occurances
- >>> sortOn (Down . snd) -- order descendingly by number of occurances
- -- Output formatting
- return $ [ wrd <> "\t" <> B.pack (show cnt) | (wrd, cnt) <- res ]
- & B.unlines
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement