[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
do

 runListT listChars2 >>= (mapM_ putChar)

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

  abcqabcq

What I want is the interaction:

  aabbccqq

Hope you understand what I mean.

/M

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
http://therning.org/magnus
-------------- 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