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

  • SEARCH
  • Home
  • 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 6955825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:47:18+00:00 2026-05-27T14:47:18+00:00

i am looking for a function which takes a function (a -> a ->

  • 0

i am looking for a function which takes a function (a -> a -> a) and a list of [Maybe a] and returns Maybe a. Hoogle gave me nothing useful. This looks like a pretty common pattern, so i am asking if there is a best practice for this case?

>>> f (+) [Just 3, Just 3]
Just 6
>>> f (+) [Just 3, Just 3, Nothing]
Nothing

Thanks in advance, Chris

  • 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-27T14:47:18+00:00Added an answer on May 27, 2026 at 2:47 pm

    You should first turn the [Maybe a] into a Maybe [a] with all the Just elements (yielding Nothing if any of them are Nothing).
    This can be done using sequence, using Maybe’s Monad instance:

    GHCi> sequence [Just 1, Just 2]
    Just [1,2]
    GHCi> sequence [Just 1, Just 2, Nothing]
    Nothing
    

    The definition of sequence is equivalent to the following:

    sequence [] = return []
    sequence (m:ms) = do
      x <- m
      xs <- sequence ms
      return (x:xs)
    

    So we can expand the latter example as:

    do x <- Just 1
       xs <- do
           y <- Just 2
           ys <- do
               z <- Nothing
               zs <- return []
               return (z:zs)
           return (y:ys)
       return (x:xs)
    

    Using the do-notation expression of the monad laws, we can rewrite this as follows:

    do x <- Just 1
       y <- Just 2
       z <- Nothing
       return [x, y, z]
    

    If you know how the Maybe monad works, you should now understand how sequence works to achieve the desired behaviour. 🙂

    You can then compose this with foldr using (<$>) (from Control.Applicative; equivalently, fmap or liftM) to fold your binary function over the list:

    GHCi> foldl' (+) 0 <$> sequence [Just 1, Just 2]
    Just 3
    

    Of course, you can use any fold you want, such as foldr, foldl1 etc.

    As an extra, if you want the result to be Nothing when the list is empty, and thus be able to omit the zero value of the fold without worrying about errors on empty lists, then you can use this fold function:

    mfoldl1' :: (MonadPlus m) => (a -> a -> a) -> [a] -> m a
    mfoldl1' _ [] = mzero
    mfoldl1' f (x:xs) = return $ foldl' f x xs
    

    and similarly for foldr, foldl, etc. You’ll need to import Control.Monad for this.

    However, this has to be used slightly differently:

    GHCi> mfoldl1' (+) =<< sequence [Just 1, Just 2]
    Just 3
    

    or

    GHCi> sequence [Just 1, Just 2] >>= mfoldl1' (+)
    Just 3
    

    This is because, unlike the other folds, the result type looks like m a instead of a; it’s a bind rather than a map.

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

Sidebar

Related Questions

for a paper I'm looking for an real-life C function which uses volatile variables.
I'm looking for a PHP library/function/class which can create Identicon s.
I am looking for a function that will tell me, for a list of
I'm new to F# and looking for a function which take N*indexes and a
I have a function which takes two arrays containing the tokens/words of two texts
I have a function which takes a string of commands to execute and makes
I'm looking for a Python function similar to nubBy in Haskell , which removes
Given a list, I'd like to divide it into clusters using a boundary function.
I need a function which takes an arbitrary number of arguments (All of the
I have a PHP function on my site which takes a couple of seconds

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.