[Haskell-beginners] Moment of enlightenment for lazy evaluation

Tom Tobin korpios at korpios.com
Sat Feb 20 20:23:48 EST 2010

On Sat, Feb 20, 2010 at 4:42 PM, Stephen Blackheath [to
Haskell-Beginners] <mutilating.cauliflowers.stephen at blacksapphire.com>
> Tom,
> The bad news is that 1. Haskell makes no guarantee about when the files
> are closed,

Hmm, Data.ByteString.Lazy.readFile's docstring says:

"Read an entire file lazily into a ByteString. The Handle will be held
open until EOF is encountered."

It certainly seemed to change matters once I switched that $ to $!,
though; I don't see why that would have helped me unless the handles
were indeed being closed.

> 2. file handles are a limited resource

Well, yes, that's why I ran into the original problem.

> and 3. lazy I/O
> doesn't handle errors in a recoverable fashion.

I suppose this will be something I'll run into before too long.

> Unfortunately this
> means that lazy I/O is fundamentally unsound.
> The only safe way to do it is to read the file strictly in blocks using
> Data.ByteString.hGet.

But with the strict version of ByteString, how would I compute the
SHA1 hash of an 8 GB file on a machine with quite a bit less memory?
I can't imagine Haskell just has no way to handle a case that other
languages handle easily.

More information about the Beginners mailing list