Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:59:41+00:00 2026-05-20T01:59:41+00:00

This should be easy for Haskell pros.. I’ve got a Maybe value, > let

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:59:41+00:00Added an answer on May 20, 2026 at 1:59 am

    pelotom’s answer is the straightforward one. But not the fun one! sequence is 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 Maybe and a monad. Data.Traversable exports a sequence function 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:

    Prelude Data.Traversable> Data.Traversable.sequence (fmap print $ Nothing)
    Nothing
    
    Prelude Data.Traversable> Data.Traversable.sequence (fmap print $ Just 123)
    123
    Just ()
    

    Note that there’s also a sequenceA function which is slightly more general, working not just on Monads but all Applicatives.

    So why use this approach? For Maybe the approach that takes it apart explicitly is fine. But what about a bigger data structure — a Map for example? In that case, traverse, sequenceA and friends from Data.Traversable can be real handy.

    Edit: as Ed’ka notes, traverse :: Applicative f => (a -> f b) -> t a -> f (t b) and so one can just write traverse print $ Just 123.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be easy for many of you, but for me it's just another
I think this should be easy, but it's evading me. I've got a many-to-many
OK this should be easy but I can't seem to get it to work.
This should be easy, but I'm not sure how to best go about it.
This should be easy, but I'm having trouble figuring out how to zoom an
OK, this should be easy, but I do not find the solution, at least
This should be easy but for some reason it isn't working. I wanted to
This should be easy, but after browsing several libraries (jquery preferred!) and even plain
This should be easy. In MySQL I could just do: select sum(column1*column2) as sum1
Good afternoon, This should be an easy one. I've done the cookie-cutter default ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.