how to do lazy IO like getContents?

Ben Escoto bescoto at stanford.edu
Sat Oct 18 22:35:40 EDT 2003


On Sat, 18 Oct 2003 19:17:06 -0700 (PDT) Hal Daume III <hdaume at ISI.EDU> wrote:
> It is unsafe because, in general, lazy IO is a bad idea.  In particular:
> 
> foo f x = do
>   h <- openFile x ReadMode
>   t <- hGetContents h
>   v <- f t
>   hClose h
>   return t
> 
> will do substantially different things depending on the strictness of
> 'f'.  For instance, if 'f' is 'return . head', you might get a
> 'head:: empty list' error, while if 'f' is 'evaluate . head', you won't.  

I don't understand the details of your example (for instance, what
does "evaluate" do?  I found a reference to it in the GHC manual under
Debug.QuickCheck, but couldn't figure out what it.), but get the
general point.

In my case, I wanted to represent a directory tree that was too big to
fit in memory.  I was planning on declaring it as

    data DirTree = DirTree File [DirTree]

but then I apparently need lazy IO.  If lazy IO is bad, is

    data DirTree = DirTree File [IO DirTree]

a better way of doing it?


P.S. Thanks for your haskell book.  I read it and learned a lot.


-- 
Ben Escoto
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://porky.devel.redhat.com/pipermail/haskell/attachments/20031018/f89f82f8/attachment.bin


More information about the Haskell mailing list