[Haskell] Files and lazyness

Tomasz Zielonka tomasz.zielonka at gmail.com
Mon Aug 1 16:38:53 EDT 2005


On Thu, Jul 28, 2005 at 08:01:17PM +0200, Diego y tal wrote:
> I understand that this is caused by the lazyness,

No, it is caused by mixing laziness with side-effects, which happens
when you use getContents/readFile.

> that doesn't evaluate the expression "x <- readFile fEntrada" until
> it's necessary,

"x <- readFile fEntrada" is not an expression.

> but.. is it normal that we have to think about this "problem" when
> programming?

You just have to know, which functions mix laziness and side-effects
by using unsafeInterleaveIO (readFile, getContents, hGetContents).

> I find this a great disadvantage as oppose of the imperative paradigm,

I wouldn't say this is a great disadvantage of Haskell, because:
a) the imperative paradigm is available in Haskell
b) readFile is a bit of hack, because it uses laziness where the order
   of execution matters (it uses unsafeInterleaveIO). It is generally
   known that readFile should be used only for the simplest I/O tasks.
c) there are other ways to complete the task (however, some of them
   are non-standard)

Best regards
Tomasz


More information about the Haskell mailing list