[Haskell-beginners] Confused about lazy IO
Kim-Ee Yeoh
ky3 at atamo.com
Fri Mar 15 00:24:35 CET 2013
On Fri, Mar 15, 2013 at 5:52 AM, Jacek Dudek <jzdudek at gmail.com> wrote:
> readFile fileName >>= \ contents ->
> writeFile fileName (f contents)
You're reading and writing to the /same/ file back to back. With Lazy
I/O. Those two just don't mix.
The "permission denied" probably stems from the attempted write
clashing with the previous exclusive read handle.
> I thought lazy IO was implemented in such a way that you were safe to
> INTERPRET the IO action as having been fully performed.
Nope.
The usual gotcha lying for the unwary is
do
h <- openFile fname1 ReadMode
s <- hGetContents h
hClose h
writeFile fname2 s
In the case fname1==fname2, a different surprise lies in store as
you've just witnessed.
Importing System.IO.Strict from the strict package should fix all of the above.
-- Kim-Ee
More information about the Beginners
mailing list