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

Magnus Therning magnus at therning.org
Sun Nov 30 02:04:12 EST 2008


Alexander Dunlap wrote:
> 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?

I think a fairly common way to solve this is to change your program to
basically do

  main = do
    h <- openFile "some-file" ReadMode
    c <- hGetContents h
    w <- openFile "temp-file"
    hPutStr w (f c)
    hClose w
    renameFile "temp-file" "some-file"

Of course there may still be some laziness issues to keep in mind.  But
I believe that should take care of any locking issues your OS might
throw at you.

/M

-- 
Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
magnus@therning.org             Jabber: magnus@therning.org
http://therning.org/magnus

Haskell is an even 'redder' pill than Lisp or Scheme.
     -- PaulPotts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20081130/d61131fa/signature.bin


More information about the Beginners mailing list