This should be easy for Haskell pros..
I’ve got a Maybe value,
> let a = Just 5
I can print it:
> print a
Just 5
But I want to apply an I/O action to the inside of the Maybe. The only way I’ve figured out how to do this without using case is:
> maybe (return ()) print a
5
However, this seems too verbose. First of all, return () is specific to the I/O monad, so I have to come up with a different “zero” for each monad I want to try this trick in.
I want to basically map an I/O action (print) onto the Maybe value and print it if it is Just, or don’t do anything if it is Nothing. I want to express it somehow like,
> fmap print a
But this doesn’t work since print is an IO action:
No instance for (Show (IO ()))
I tried Applicative, but can’t figure out if there’s a way to express it:
> print <$> a
No instance for (Show (IO ()))
Obviously I’m a bit confused about monads-inside-monads.. can anyone tell me the right way to most succinctly express this?
Thanks.
pelotom’s answer is the straightforward one. But not the fun one!
sequenceis the Haskell function that one can think of as flipping the order of type constructors between a list and a monad.sequence :: (Monad m) => [m a] -> m [a]Now what you want is, so to speak, to flip the order of type constructors between a
Maybeand a monad. Data.Traversable exports asequencefunction with just that capacity!Data.Traversable.sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)This can specialize to
Maybe (IO ()) -> IO (Maybe ())like in your example.Hence:
Note that there’s also a
sequenceAfunction which is slightly more general, working not just on Monads but all Applicatives.So why use this approach? For
Maybethe approach that takes it apart explicitly is fine. But what about a bigger data structure — aMapfor example? In that case,traverse,sequenceAand friends fromData.Traversablecan be real handy.Edit: as Ed’ka notes,
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)and so one can just writetraverse print $ Just 123.