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

Brent Yorgey byorgey at seas.upenn.edu
Sun Mar 31 13:30:08 CEST 2013


To elaborate a bit: you originally said "I don't like that the line
processing I'm doing will have to be in the IO monad" -- but since you
want the processing of the list of Strings to be interleaved with IO
operations, you have no choice.  Pure functions of type [String] ->
[String] cannot have their evaluation interleaved with IO operations.

Note that functions using lazy I/O such as getContents, readFile,
etc. can create exceptions to this -- but these use unsafeInterleaveIO
under the hood and are widely regarded as problematic.  So you could
try using unsafeInterleaveIO, but it is unsafe for a reason.  I do not
actually know what is required to ensure you are using it safely;
perhaps someone else could elaborate on this.

-Brent

On Sun, Mar 31, 2013 at 01:48:23PM +0300, Ovidiu D wrote:
> Right. I definitely want the lazy behavior.
> 
> Thanks
> 
> 
> On Sun, Mar 31, 2013 at 1:29 PM, Brent Yorgey <byorgey at seas.upenn.edu>wrote:
> 
> > Unfortunately, with the definition
> >
> >   f = getLine : f
> >
> > this will not work.  'sequence f' has to do *ALL* the IO before you
> > can process even the first String in the resulting list.  Since it is
> > infinite, it will just sit there reading lines forever but never
> > letting you process them.
> >
> > I think in this case using [IO String] is actually a good solution.
> >
> > -Brent
> >
> > On Sun, Mar 31, 2013 at 12:24:33PM +0200, Nathan Hüsken wrote:
> > > Try
> > >
> > > sequence :: Monad m => [m a] -> m [a]
> > >
> > > I thinkg
> > >
> > > sequence f :: IO [String]
> > >
> > > should be what you want.
> > >
> > > On 03/31/2013 12:19 PM, 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
> > > >
> > >
> > >
> > > _______________________________________________
> > > Beginners mailing list
> > > Beginners at haskell.org
> > > http://www.haskell.org/mailman/listinfo/beginners
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >

> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list