Skip to content

Instantly share code, notes, and snippets.

@mendezcode
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendezcode/c6408b079e4cdc154bf7 to your computer and use it in GitHub Desktop.
Save mendezcode/c6408b079e4cdc154bf7 to your computer and use it in GitHub Desktop.
Small haskell program to trim whitespace in files
import System.Environment (getArgs)
import System.Directory (doesFileExist)
import Data.Char (isSpace)
main :: IO ()
main = do
args <- getArgs
case args of
[] -> putStrLn msg
[_] -> putStrLn msg
[file, outFile] -> processFile file outFile
otherwise -> putStrLn "Too many arguments"
where
msg = "Not enough arguments"
type Filename = String
processFile :: Filename -> Filename -> IO ()
processFile file outFile = do
exists <- doesFileExist file
case exists of
False -> putStrLn $ "File does not exist: " ++ file
True -> do
buf <- readFile file
let str = foldr cleanLine [] (lines buf)
writeFile outFile $ unlines str where
cleanLine line xs = if success then (empty:xs) else (line:xs) where
success = all isSpace line
empty = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment