[Haskell-cafe] Re: understanding enumerator/iteratee

oleg at okmij.org oleg at okmij.org
Tue Dec 23 03:45:33 EST 2008

Artyom Shalkhakov wrote
> I would say that it [iteratee] just tells us how to react to various forms of
> input. :) This is much like the function you pass to foldr.

Precisely. To sum up all elements of Data.Map, we do
	Map.fold (+) 0 mp

to sum up all elements of a set we do
	Set.fold (+) 0 st

ditto for any other foldable data structure. Clearly the function that
sums the current element with the accumulator, (+), doesn't know or
care from which collection the elements are coming from. The initial
seed, 0, is again unaware of the collection.

Iteratee is indeed the function that you pass to fold (combined with
the seed for practical reasons). One may conceptually consider
iteratee to be a pair of the function to feed to fold, and the initial
seed (the accumulator in the above example). That achieves the
separation of concerns: fold (aka, enumerator) has the intimate knowledge
of the collection and how to get to the next element; iteratee knows
what to do with the current element.

More information about the Haskell-Cafe mailing list