Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I apply a series of monadic actions to the same argument in Haskell?

In Haskell, is there a way to simplify the following code to remove the repeating of "hello world"? In other words, applying f, g, and h to hello world?

fn = do
  a <- f "hello world"
  b <- g "hello world"
  c <- h "hello world"
  return (a, b, c)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If f, g and h have the same type, you can simply loop over them:

  [a,b,c] <- forM [f,g,h] ($"hello world")

If they have different types, you can use the fact that functions form a monad, and in particular can be stacked as a monad transformer on top of your current monad:

import Control.Monad.Trans.Reader

fn = (`runReaderT`"hello world") $ do
    a <- ReaderT f
    b <- ReaderT g
    c <- ReaderT h
    return (a, b, c)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading