[Haskell-cafe] IO in lists

Magnus Therning magnus at therning.org
Tue Jan 16 09:13:41 EST 2007

On Tue, Jan 16, 2007 at 14:06:08 +0200, Yitzchak Gale wrote:
>But the list monad [] is not a transformer, so you can't lift in it,
>even if the contained type happens also to be a monad.

Yeah, I had some vague thought of that being a problem, which lead me to
ListT.  Your statement put some good words to that thought.

>Perhaps you are looking for something like this, using the monad
>transformer version of []:
>listChars2 :: ListT IO Char
>listChars2 = do
> c <- lift getChar
> if c == 'q'
>   then return [c]
>   else return [c] `mplus` listChars2
>GHC finds this much more tasty, and then
>runListT listChars2
>does what I think you may want.

Yes, that is fairly close to what I want to do.  Now, it'd be really
good if I could apply a function to each item in the ListT, with the
constraint that it should be done lazily.  I.e. the following will not

 runListT listChars2 >>= (mapM_ putChar)

because it first reads the input until 'q' is pressed.  I.e. this
produces the interaction:


What I want is the interaction:


Hope you understand what I mean.


P.S. I'm aware a reqursive solution is possible and I've already coded
one that works.  I'm just curious if a "map solution" is possible.

Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
magnus at therning.org             Jabber: magnus.therning at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070116/7eaf048b/attachment.bin

More information about the Haskell-Cafe mailing list