[Haskell-beginners] [IO String] to IO [String]

Tony Morris tonymorris at gmail.com
Sun Mar 31 12:23:14 CEST 2013


You can use sequence which will turn your [IO String] into a IO
[String]. If you want to "map a function" along the way, you can use
mapM (or traverse):

That is:
mapM :: (String -> IO b) -> [IO String] -> IO [b]

For further reading, there is a paper about this function called The
Essence of the Iterator Pattern.

On 31/03/13 20:19, Ovidiu D wrote:
> I have the function f which reads lines form the stdin and looks like
> this:
>
> f :: [IO String]
> f = getLine : f
>
> What I don't like is the fact that the line processing I'm doing will
> have to be in the IO Monad
>
> I would like to make this function to have the signature
> f : IO [String]
> ...such that I can get rid of the IO monad and pass the pure string
> list to the processing function.
>
> Can I do this?
>
> Thanks
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


-- 
Tony Morris
http://tmorris.net/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130331/474ce7d9/attachment.htm>


More information about the Beginners mailing list