[Haskell-beginners] [Haskell-Beginners] Laziness & referential transparency

Daniel Fischer daniel.is.fischer at googlemail.com
Sat Jan 29 16:53:55 CET 2011


On Saturday 29 January 2011 16:36:09, Tim Baumgartner wrote:
> Daniel,
>
> thanks for your explanations! For some time, I thaught laziness might be
> some kind of holy grail.

There is no such thing.
Laziness makes it possible to write many algorithms in a concise and 
elegant manner which can't be so expressed in a strict language.
But on the other hand, laziness makes it harder to predict performance and 
memory usage (experience helps a lot with that).

> But now I realize that it takes both a lot of
> time to understand properly and that it has more limitations than I
> expected. Another example for this: I wrote a function
>
> recursiveContents :: FilePath -> IO [FilePath]
>
> and later on I thaught it was possible to run some actions on every file
> from the list, in a lazy fashion. Then I had to find out that all files
> had to be found before I could process the first one.

That depends. Often you can use unsafeInterleaveIO (which is not nearly as 
unsafe as unsafePerformIO) to get the list lazily even in IO. Then, if your 
consumption pattern allows it, you can process the first file before the 
second is found (if, however, processing the files can influence the result 
of recursiveContents, you can see why there is an unsafe in 
unsafeInterleaveIO).

> By now, I think I
> understand why, but it stops me from being too enthusiastic about
> laziness

Well, as with most things, there are advantages and disadvantages.

>
> :-(
>
> Regards
> Tim




More information about the Beginners mailing list