[Haskell-beginners] Confused about lazy IO

Gesh hseG gesh at gesh.uni.cx
Sun Mar 17 15:10:37 CET 2013


That would have been useful to know three weeks ago.
Better late than never I guess.
Thanks.


On Fri, Mar 15, 2013 at 1:24 AM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:

> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130317/5945bf4b/attachment-0001.htm>


More information about the Beginners mailing list