[Haskell-beginners] A first try
Yitzchak Gale
gale at sefer.org
Mon Jun 27 11:54:51 CEST 2011
Heinrich Apfelmus wrote:
> Again, let me stress that the biggest drawback of the Iteratee approach is
> that you have to rewrite the consumer and cannot reuse ordinary list
> functions like length , words , lines , and so on. But these functions are
> already lazy, so why not make use of this. (You don't lose anything, every
> Iteratee can be rewritten as an ordinary function String -> IO a by using
> `seq` in corresponding places.)
> Here one possibility for a lazier version of withFile' :
Let me just add my voice in support of Heinrich.
There has been a lot of FUD lately about lazy IO being
"unsafe" or even "evil". This is just not so.
When used correctly, there is nothing unsafe about lazy IO.
It's a matter of taste in the end, I suppose, but to me
lazy IO is the most natural way of doing IO in Haskell. True,
there are some subtleties. These stem from the fact that,
like Iteratees and any other current approach, they involve
superimposing operational semantics on top of Haskell.
I find that code written using lazy IO is clearer, more natural,
and easier to write than when written using
Iteratees/Enumeratees, even in more complicated cases when
you need to do some work to get the laziness to match up.
I have great respect for Oleg's enlightening research on
Iteratees. But for practical real-life programming, I choose
lazy IO over Iteratees when I have the choice.
Whether you are using lazy IO or Iteratees, we really need
some better higher-level combinators with simpler semantics
for more of the common use cases. Then it won't really matter.
And yes, that is possible with Iteratees too. See for, example,
Text.XML.Enumerator.Parse in Michael Snoyman's
xml-enumerator package. (But he conspicuously omits the ugly
type signatures in his sample usage. Those could use some
wrapping.)
Regards,
Yitz
More information about the Beginners
mailing list