Skip to content

Instantly share code, notes, and snippets.

@quephird
Created April 12, 2015 03:17
Show Gist options
  • Save quephird/5fe2620e6183d52a64f2 to your computer and use it in GitHub Desktop.
Save quephird/5fe2620e6183d52a64f2 to your computer and use it in GitHub Desktop.
Using the supplied interactWith function, write another function that writes the first word of each line to another file.
interactWith :: (String -> String) -> FilePath -> FilePath -> IO ()
interactWith f inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (f input)
writeFirstWords inputFile outputFile = interactWith firstWords inputFile outputFile where
firstWord "" = ""
firstWord line = head $ words line
firstWords file = unlines $ map firstWord $ lines file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment