Friday 15 October 2010

code golf - wordCount

An example for one of the courses taught here, was a program that counts the number of occurrences of unique words in a file. It was written in Python, and was clean and concise enough.

Following is a Haskell version:

import Control.Arrow((&&&))
import Data.List(group,sort)

wordCount :: String -> [(String,Int)]
wordCount = map (head &&& length)

          . group . sort . words