An Improved Reverse Words Script


About 6 months ago I posted a solution to the Google Code Jam problem Reverse Words

I’ve become more comfortable with Haskell since then, so here’s an improved solution.

module Main where

Problem statement:

Run as a script with runghc, or compile with ghc --make -O2

input and output is by redirection;

$ ./reverse_words < input_file > output_file

import Control.Monad

main :: IO ()
main = do
  n <- readLn
  forM_ [1..n] $ \i -> do
    solution <- fmap (unwords . reverse . words) getLine
    putStrLn $ "Case #" ++ show i ++ ": " ++ solution
Improved Reverse Words With Haskell view as gist

I’m not one who believes that shorter, terser code is necessarily better, but that isn’t what makes this version an improvement. Nor even the use of Monads; Haskell’s sexy buzzword. I think this code is better because it has less noise than the original solution.

Code Jam problems have well defined inputs and required outputs, and there is no need to load the whole input file when the script starts. Normally, I would be all for splitting out code into smaller, descriptively named functions, but in this small case that’s just more typing and more lines getting in the way of the actual solution. I think that this version is more readable because there are less lines of code that don’t contribute directly to the solution.

And Code Jam has a time limit, so smaller code is faster to write and easier to debug.