Simon Marlow wrote:
> 
> > Arguably, readFile is too strict: why is the file opened, if there
> > aren't any characters needed. What is the motivation for opening the
> > file eagerly?
> 
> Once we've started reading lazilly, it's hard to generate errors.  The file
> is opened eagerly so that any errors generated (such as if the file doesn't
> exist) can be propogated immediately to the caller.
> 
> I checked the Haskell Report, which doesn't seem to specify the correct
> behaviour - I guess this should be clarified.  The report also contains the
> sentence "The exceptions raised by the I/O functions in the Prelude are
> defined in the Library Report", but I can't find any reference to
> readFile/writeFile in the library report.
> 
> Cheers,
>         Simon

I can't seem to work around this behaviour, is it possible to
force readFile to just open a file, read it entirely and close it
before doing the next open ? I tried a few things with seq,
but this doesn't work:

        xs <- mapM (\x -> let y = readFile x
                          in seq y y) (take 1000 (repeat "tmp"));

and neither does this:

        let
          f = mapM (\x -> let y = readFile x
                          in seq y y) (take 1000 (repeat "tmp"));
        in do {
                xs <- seq f f;
                return ();
        }

  Jan

Reply via email to