[Haskell-cafe] Re: Lazy IO and closing of file handles

Pete Kazmier pete-expires-20070513 at kazmier.com
Mon Mar 19 13:25:50 EDT 2007

Pete Kazmier <pete-expires-20070513 at kazmier.com> writes:

> I attempted to read Oleg's fold-stream implementation [1] as this
> sounds quite appealing to me, but I was completely overwhelmed,
> especially with all of the various type signatures used.  It would be
> great if one of the regular Haskell bloggers (Tom Moertel are you
> reading this?) might write a blog entry or two interpreting his
> implementation for those of us starting out in Haskell perhaps by
> starting out with a non-polymorphic version so as to emphasize the
> approach.
> [1] http://okmij.org/ftp/Haskell/fold-stream.lhs

In the event any other Haskell newbie comes along someday and is just
as overwhelmed as I was, I've found this post by Oleg to be a much
easier to understand than the above paper because it is not as generic
and thus the type signatures are a bit easier on the eyes:


With that said, I have a question regarding Hal's response to the
above email in which he states:

> Just thought I'd mention that this is, in fact, my preferred method of
> iterating over a file.  It alleviates the pain associated with lazy file
> IO, and simultaneously provides a useful abstraction.  I actually have
> 3*2 functions that I use which look like:
> > type Iteratee  iter seed = seed -> iter -> Either seed seed
> > hFoldChars  :: FilePath -> Iteratee  Char     seed -> seed -> IO seed
> > hFoldLines  :: FilePath -> Iteratee  String   seed -> seed -> IO seed
> > hFoldWords  :: FilePath -> Iteratee  [String] seed -> seed -> IO seed
> > type IterateeM iter seed = seed -> iter -> IO (Either seed seed)
> > hFoldCharsM :: FilePath -> IterateeM Char     seed -> seed -> IO seed
> > hFoldLinesM :: FilePath -> IterateeM String   seed -> seed -> IO seed
> > hFoldWordsM :: FilePath -> IterateeM [String] seed -> seed -> IO seed
> Which perform as expected (hFoldWords(M) can be written in terms of
> hFoldLinesM, but I find I use it sufficiently frequently to warrent
> having it stand out).  Also, of course, the only ones actually
> implemented are the (M) variants; the non-M variants just throw a return
> into the Iteratee.

What does he mean by the very last sentence?  Oleg's version seems
more like the non-M versions.  What is his implication?


More information about the Haskell-Cafe mailing list