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 6664065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:34:07+00:00 2026-05-26T02:34:07+00:00

findM :: Monad m => (a -> m Bool) -> m [a] -> Maybe

  • 0
findM :: Monad m => (a -> m Bool) -> m [a] -> Maybe (m a)

I cannot implement it by myself. I could use some pointers

find looks like:

find f as = listToMaybe $ filter f as

so I tried:

findM f as = filterM f as >>= listToMaybe

but it doesnt work.

  • 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-26T02:34:08+00:00Added an answer on May 26, 2026 at 2:34 am

    No. It is not possible. However, you can write a function with the signature:

    findM :: Monad m => (a -> m Bool) -> m [a] -> m (Maybe a)
    

    The reason it is not possible is because in order to figure out if the Maybe has constructor Just x or Nothing, you have to get the value out of the monad. Which means that the Maybe must be inside the monad at the end, since it is not possible in general to extract a value from inside a monad. (That is the whole point of monads, after all.)

    For example, given your signature to findM, you could write some obviously incorrect functions:

    findM :: Monad m => (a -> m Bool) -> m [a] -> Maybe (m a)
    findM = magic
    anyM :: Monad m => (a -> m Bool) -> m [a] -> Bool
    anyM f = isJust . findM f
    extractBool :: Monad m => m Bool -> Bool
    extractBool = anyM id . liftM (:[])
    

    The function extractBool is obviously impossible, so findM cannot have that signature.

    Here is an implementation of findM:

    findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
    findM _ [] = return Nothing
    findM f (x:xs) = do y <- f x
                        if y then return (Just x) else findM f xs
    

    I am not sure of a simpler way to implement it — this seems fairly clean, and it works on infinite lists.

    Changing the signature from using an m [a] to using [a] makes it more useful and easier to use. You’ll quickly figure out why, if you implement both interfaces.

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

Sidebar

Related Questions

I find myself adding debugging print statements quite often -- stuff like this: print(a_variable_name:
Let's say we want to use ReaderT [(a,b)] over the Maybe monad, and then
I find myself doing more and more scripting in haskell. But there are some
I find myself writing delegates occasionally for really simple functions (take no arguments and
I usually use ASP.net web forms for GUI, maybe one of most stateful technologies.
I find some sample code about PNS, article here and I also create an
Find many authors interchangably use the term Type and Class. Certain textbooks covering object
Find the maximum of two numbers. You should not use if-else or any other
find same node from two single linked lists. Can't use hash, Can not be
I'm surprised that I could not find any info on this. I must be

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.