-- Print a histrogram of the tokens in the standard input stream
module Main where

import Text.Printf (printf)
import Data.List (sort, groupBy)

{-
   (.)      :: (a->b) -> (c->a) -> c -> b  ;  function composition (infix)
   words    :: String->[String]            ; breaks a string into words at whitespace
   unlines  :: [String] -> String          ; join string list with newlines
   interact :: (String -> String) -> IO () ; the input and output are strings
   groupBy  :: (a -> Bool) -> [a] -> [[a]] ; collect together elments by the given predicate
   map      :: a->b -> [a] ->[b]           ; apply function to every item in sequence
-}

main :: IO()
main = interact (unlines . map format . count . words)

format :: (String,Int) -> String
format (word, count) = printf "%4d: %s" word count

count :: [String] -> [(String,Int)]
count = map (\ws->(head ws, length ws))  . groupBy (==)  . sort

--   ------------For GNU Emacs ------------
--  Local Variables:
--  compile-command: "ghc -Wall words.hs"
--  End: