countWords.hs

-- | A program to count words. You can compile it to an executable file
-- with 'ghc -O countWords.hs' 
-- Functional Programming course 2018.
-- Thomas Hallgren

{-
This started as a skeleton, the definitions were filled in
during the lecture.
-}

module Main where --This can be omitted, since the default module name is Main

import Data.List(group,sort)

main :: IO ()
main = interact wordCounts

-- | Count words in a text and produce nicely formatted output
wordCounts :: String -> String
wordCounts = unlines
           . map (\(n,w)->w++": "++show n)
           . reverse
           . sort
           . map (\ws-> (length ws,head ws))
           . group
           . sort
           . words


-- Reminder: (.) is function composition, defined as:
-- (f . g) x = f (g x)

Plain-text version of countWords.hs | Valid HTML?