#!/usr/bin/runhugs module Main(main) where import System(getArgs) main :: IO () main = do args <- getArgs if null args then wcFiles ["-"] else wcFiles args wcFiles :: [String] -> IO () wcFiles [] = return () wcFiles ("-" : xs) = do input <- getContents wcFile "-" input wcFiles xs wcFiles (x : xs) = do contents <- catch (readFile x) (\e -> do putStrLn ("ERROR reading" ++ " file: " ++ x) return "") wcFile x contents wcFiles xs wcFile :: String -> String -> IO () wcFile fname contents = putStrLn (lineCnt ++ wordCnt ++ charCnt ++ " " ++ fname) where ls = lines contents lineCnt = rjustify 8 (show (length ls)) wordCnt = rjustify 8 (show (sum (map (length . words) ls))) charCnt = rjustify 8 (show (length contents)) rjustify :: Int -> String -> String rjustify n s = space (n - length s) ++ s space :: Int -> String space n = replicate n ' '