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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:27:13+00:00 2026-05-27T18:27:13+00:00

I am a beginner with haskell and am reading the Learn you a haskell

  • 0

I am a beginner with haskell and am reading the Learn you a haskell book. I have been trying to digest functors and applicative functors for a while now.

In the applicative functors topic, the instance implementation for Maybe is given as

instance Applicative Maybe where
  pure = Just
  Nothing <*> _ = Nothing
  (Just f) <*> something = fmap f something

So, as I understand it, we get Nothing if the left side functor (for <*>) is Nothing. To me, it seems to make more sense as

  Nothing <*> something = something

So that this applicative functor has no effect. What is the usecase, if any for giving out Nothing?

Say, I have a Maybe String with me, whose value I don’t know. I have to give this Maybe to a third party function, but want its result to go through a few Maybe (a -> b)‘s first. If some of these functions are Nothing I’ll want them to silently return their input, not give out a Nothing, which is loss of data.

So, what is the thinking behind returning Nothing in the above instance?

  • 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-27T18:27:14+00:00Added an answer on May 27, 2026 at 6:27 pm

    How would that work? Here’s the type signature:

    (<*>) :: Applicative f => f (a -> b) -> f a -> f b
    

    So the second argument here would be of type Maybe a, while the result needs to be of type Maybe b. You need some way to turn a into b, which you can only do if the first argument isn’t Nothing.

    The only way something like this would work is if you have one or more values of type Maybe (a -> a) and want to apply any that aren’t Nothing. But that’s much too specific for the general definition of (<*>).


    Edit: Since it seems to be the Maybe (a -> a) scenario you actually care about, here’s a couple examples of what you can do with a bunch of values of that type:

    Keeping all the functions and discard the Nothings, then apply them:

    applyJust :: [Maybe (a -> a)] -> a -> a
    applyJust = foldr (.) id . catMaybes
    

    The catMaybes function gives you a list containing only the Just values, then the foldr composes them all together, starting from the identity function (which is what you’ll get if there are no functions to apply).

    Alternatively, you can take functions until finding a Nothing, then bail out:

    applyWhileJust :: [Maybe (a -> a)] -> a -> a
    applyWhileJust (Just f:fs) = f . applyWhileJust fs
    applyWhileJust (Nothing:_) = id
    

    This uses a similar idea as the above, except that when it finds Nothing it ignores the rest of the list. If you like, you can also write it as applyWhileJust = foldr (maybe (const id) (.)) id but that’s a little harder to read…

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

Sidebar

Related Questions

I am a beginner interested in Haskell, and I have been trying to implement
While solving some Project Euler Problems to learn Haskell (so currently I'm a completly
Haskell beginner here. I want to pass a type parameter with JSON, and have
I have a beginner question about dates and String in Haskell. I need to
I'm a Haskell beginner and have to define a series of known statuses that
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
I'm playing around with beginner Haskell, and I wanted to write an average function.
I am a beginner in Haskell so please bear with me. (Just started learning
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner help needed :) I am doign an example form a php book which

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.