[Haskell-beginners] In-place lazy I/O

Alexander Dunlap alexander.dunlap at gmail.com
Sat Nov 29 19:43:22 EST 2008

Hi all,

Suppose my program has one or more persistent files that it reads at
or near the beginning of its execution and writes at or near the end.
Thus, an extremely simplified model of my program could be

> main = do
>   h <- openFile "some-file" ReadMode
>   c <- hGetContents h
>   w <- openFile "some-file" WriteMode
>   hPutStr w (f c)

where f is some arbitrary function.

My question is how to do this. It seems like the data from h won't
necessarily be forced until f is called when it is written, and there
is no guarantee that all of the data will even be used by f until it
is written. Thus, I will get a file-locking error when trying to write
the file. Do I have to rig things so that I know f will consume all of
its input? Is it better to use strict I/O? Is there a better idiom for
this entirely?

Thanks for all of your help.

More information about the Beginners mailing list