18 Feb 10:57
new Haskell hacker seeking peer review
From: Sean Perry <shaleh <at> speakeasy.net>
Subject: new Haskell hacker seeking peer review
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2005-02-18 09:58:56 GMT
Subject: new Haskell hacker seeking peer review
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2005-02-18 09:58:56 GMT
I am learning Haskell, so I decided to implement everyone's favorite,
overused Unix command -- cat. Below is my simple implementation,
comments about style, implementation, etc. are welcomed.
In particular, is my untilEOF idiomatically ok? Is there a better way to
accomplish this? Also, while talking about untilEOF, it is slightly
annoying that hIsEOF returns IO Bool and that functions like 'not' only
want Bool. Sure makes the logic tests feel like more work than they
should be.
cat.hs:
module Main where
import IO
import System(getArgs)
untilEOF :: Handle -> (Handle -> IO ()) -> IO ()
untilEOF hdl f = do eof <- hIsEOF hdl
if eof then return ()
else do f hdl
untilEOF hdl f
cat :: Handle -> IO ()
cat hdl = do line <- hGetLine hdl
putStrLn line
catFile :: FilePath -> IO ()
catFile path = do hdl <- openFile path ReadMode
untilEOF hdl cat
(Continue reading)
RSS Feed