Reverse Words With Haskell

A practice problem from Google Code Jam.

As part of my preparation for Google Code Jam I’ve been working through the practice problems with Haskell.

I made a screencast on solving the Reverse Words problem, along with the mechanics of getting the problem inputs and submitting solutions. The style was heavily influenced by a sequence on Data Driven Programming in Haskell by Jonas Tullus

I moved at a slower pace, and talking and typing seem to have confused my brain so I think I lapsed into gibberish in a couple of places. (If you know me, you’ll know that’s nothing new).

Here’s the cleaned up source file.

module Main where

 - Problem Statement:
 - Usage either compile or use runhaskell / runghc
 - Pass the input file as the sole command line argument
 - Redirect output if you want the results to go in a file

import IO
import System.Environment
import Data.List

reverseWords :: String -> String
reverseWords = unwords . reverse . words

boilerPlate :: [String]
boilerPlate = ["Case #" ++ show n ++ ": " | n <- [1..]]

standardOutput :: [String] -> [String]
standardOutput = zipWith (++) boilerPlate

main =  do
  (f:_) <- getArgs
  file  <- readFile f

  let cases     = tail $ lines file
      solutions = standardOutput $ map reverseWords cases
  putStrLn $ unlines $ solutions

I need to learn more Emacs shortcuts :(