[Haskell-cafe] mixing map and mapM ?

Neil Brown nccb2 at kent.ac.uk
Thu May 6 07:00:01 EDT 2010

Bill Atkins wrote:
> Almost - "liftM modificationTime" has type Status -> IO EpochTime. 
>  Like other IO functions (getLine, putStrLn), it returns an IO action 
> but accepts a pure value (the modification time)
> Also, I like this style:
> import Control.Applicative ((<$>))
> blah = do 
>   times <- mapM (PF.modificationTime <$> PF.getFileStatus) filenames
>   ...
> The <$> operator evaluates to fmap so it's a cleaner way to apply a 
> pure function to an IO value.
That won't type-check (unless I've missed some crafty trick with the 
types?!); you have two functions you want to compose, but the <$> 
operator (i.e. fmap) applies a function on the left to a functor-value 
on the right.  You would instead need:

times <- mapM ((PF.modificationTime <$>) . PF.getFileStatus) filenames

At which point I prefer Ivan's liftM version rather than the above 
section (or worse: using (<$>) prefix).  The original request is a 
relatively common thing to want to do, so I was slightly surprised that 
hoogling for:

(b -> c) -> (a -> f b) -> a -> f c

didn't turn up any relevant results.  This function is a lot like (<=<) 
but with a pure rather than side-effecting function on the left-hand side.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100506/1017b0cb/attachment.html

More information about the Haskell-Cafe mailing list