Last active
August 29, 2015 14:04
-
-
Save mendezcode/c6408b079e4cdc154bf7 to your computer and use it in GitHub Desktop.
Small haskell program to trim whitespace in files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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