Hello!

On Mon, May 31, 1999 at 06:01:31PM +0200, Friedrich Dominicus wrote:
> Hannah Schroeter wrote:

> > Hello!

> > On Fri, May 28, 1999 at 08:00:27AM +0200, Friedrich Dominicus wrote:
> > > I wrote before with my trouble understanding hugsIsEOF. But I don't have
> > > found a clean way just to write a cat. Can s.o give me a hand?

> > import System(getArgs)
> > file2stdout :: String {- filename -} -> IO ()

> could you explain that to me?

"{- filename -}" is just a comment designating that that parameter
shall be the filename of the file to be copied to stdout.

> If I want to do it line-by-line is is some combination from
> getLine, putStr ?

If you want to do it line wise, you probably have to do some
exception handling in the IO monad. I.e. you try to read a line,
handle the EOF exception by just terminating, any other exception
by re-throwing it. If getLine succeeds you output it and continue,
using tail recursion.

That's something like this:

import IO (isEOFError,openFile,IOMode(ReadMode),hGetLine)

file2stdout filename = catch mainloop handler
  where
    mainloop = do
      handle <- openFile filename ReadMode
      mainloop' handle
    mainloop' hdl = do
      line <- hGetLine hdl
      putStrLn line
      mainloop' hdl
    handler err = if isEOFError err then return () else ioError err -- rethrow

But why make it difficult if there's readFile?

> Regards
> Friedrich

Regards, Hannah.


Reply via email to