[Haskell-cafe] [iteratee] how to do nothing .. properly

Sergey Mironov ierton at gmail.com
Thu Jun 2 00:46:32 CEST 2011

Hi. Would anybody explain a situation with iter6 and iter7 below?
Strange thing - first one consumes no intput, while second consumes it
all, while all the difference is peek  which should do no processing
(just copy next item in stream and return to user).
What I am trying to do - is to write an iteratee consuing no input,
but returning a constant I give to it. I thought (return a) should do
it, but it seems I was wrong as return actually consumes all unparsed
stream. iter6 experience tells me that (peek>>return a) is what I
need, but it's completely confusing and not what I expected.


 import Data.Iteratee as I
 import Data.Iteratee.IO
 import Control.Monad
 import Control.Exception
 import Data.ByteString
 import Data.Char
 import Data.String

 -- countBytes :: (..., Num b) => Iteratee s m a -> Iteratee s m (a, b)
 countBytes i = enumWith i I.length

 iter6 = do
     h <- countBytes $ (peek >> return 0)
     s <- I.stream2list
     return (h,s)

 iter7 = do
     h <- countBytes $ (return 0)
     s <- I.stream2list
     return (h,s)

 print6 = enumPure1Chunk [1..10] (iter6) >>= run >>= print
 print7 = enumPure1Chunk [1..10] (iter7) >>= run >>= print

Here is example ghci session

*Main> print6
-- read 0 items, returns 0
*Main> print7
-- read 10 items (???) returns 0

More information about the Haskell-Cafe mailing list