how to do lazy IO like getContents?

Hal Daume III hdaume at ISI.EDU
Sat Oct 18 20:17:06 EDT 2003

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.  

 Hal Daume III                                   | hdaume at
 "Arrest this man, he talks in maths."           |

On Sat, 18 Oct 2003, Ben Escoto wrote:

> On Sun, 19 Oct 2003 02:03:58 +0200 Nick Name < at> wrote:
> > You have to use unsafeInterleaveIO, wich lazily defers the IO action 
> > passed as an argument. Look for this function in your documentation, 
> > both hugs and ghc have it.
> Got it, thanks.  Do you know in what sense it is "unsafe" though?  Are
> there some pitfalls I should be aware of?
> -- 
> Ben Escoto

More information about the Haskell mailing list