Skip to content

Instantly share code, notes, and snippets.

@Abizern
Created April 12, 2012 16:16
Show Gist options
  • Save Abizern/2368757 to your computer and use it in GitHub Desktop.
Save Abizern/2368757 to your computer and use it in GitHub Desktop.
Solution to Google Code Jam puzzle "Reverse Words" in Haskell
module Main where
{-
- Problem Statement:
- http://code.google.com/codejam/contest/351101/dashboard#s=p1
-
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment