3
\$\begingroup\$

I'm trying to implement function downpairs which takes monad of pairs and transform it into pair of monad

My current implementation is

import Control.Monad
import Control.Arrow
import Data.Maybe

downpair :: (Monad m) => m (a, b) -> (m a, m b)
downpair = ((return . fst =<<) &&& (return . snd =<<)) 

main = do
    print $ downpair $ Just ("Hello", "World")

Is there way to implement downpair in more compact maner?

PS Sorry for my poor English

\$\endgroup\$
5
\$\begingroup\$

(return . fst =<<) can be replaced by fmap fst. Your definition then becomes

downpair = fmap fst &&& fmap snd
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.