Skip to content

Instantly share code, notes, and snippets.

@dtjm
Created July 13, 2012 16:49
Show Gist options
  • Save dtjm/3105934 to your computer and use it in GitHub Desktop.
Save dtjm/3105934 to your computer and use it in GitHub Desktop.
The Cat in the Haskell
import Control.Monad as Monad
import System.Exit
import System.IO as IO
import System.Environment as Env
main :: IO ()
main = do
-- Get the command line arguments
args <- Env.getArgs
-- If we have arguments, read them as files and output them
if (length args > 0) then catFileArray args
-- Otherwise, output stdin to stdout
else catHandle stdin
catFileArray :: [FilePath] -> IO ()
catFileArray = Monad.mapM_ catFile
catFile :: FilePath -> IO ()
catFile f = openFile f ReadMode >>= catHandle
catHandle :: Handle -> IO ()
catHandle h = hGetContents h >>= putStr
@dtjm
Copy link
Author

dtjm commented Jul 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment