[Haskell-beginners] How do I map a String and a IO String?

Francesco Ariis fa-ml at ariis.it
Thu Jun 29 14:48:46 UTC 2017


On Thu, Jun 29, 2017 at 04:15:16PM +0200, Jona Ekenberg wrote:
> Thank you for your help Francesco!
> 
> I tried writing it like this:
> 
> > lineOrIo :: String -> IO String
> > lineOrIo cs | (isPrefixOf "./" cs) = readFile cs
> >             | otherwise            = return cs
> >
> > printLines path = do
> >   file <- readFile path
> >   lines <- map lineOrIo (lines file)
> >   print lines

You are using `map`, which has signature

    λ> :t map
    map :: (a -> b) -> [a] -> [b]

But lineOrIo hasn't signature `a -> b` but `a -> m b` (where m is a monad)!
mapM will fit the bill:

    mapM :: Monad m => (a -> m b) -> [a] -> m [b]

and that should do it!


More information about the Beginners mailing list