[Haskell-beginners] mapM etc.
Sean Perry
shaleh at speakeasy.net
Sat Jul 2 07:23:36 CEST 2011
On Jul 1, 2011, at 1:08 PM, Dennis Raddle wrote:
> It's been about a year since I used Haskell and I'm rusty. I think I knew how to do this once, but I need a reminder.
>
> I've got some functions:
>
> getDeviceInfo :: Int -> IO DeviceInfo
>
> name :: DeviceInfo -> String
>
> I want to do something like
>
> func :: IO ()
> func = do
> ds <- mapM getDeviceInfo [0..10]
> mapM_ (print . name) ds
>
> Is there a way to combine 'getDeviceInfo', 'name', and 'print' in one line?
>
I end up with either:
join . fmap (mapM_ (print . name)) $ mapM getDeviceInfo [1..10]
or
mapM getDeviceInfo [1..10] >>= mapM_ (print . name)
You can move the call to name into the mapM using (fmap name . getDeviceInfo) but I left them separate to match your code better.
More information about the Beginners
mailing list