Hacker News new | past | comments | ask | show | jobs | submit login

That was a poor choice. The local “a” shadows the parameter “a”. You can of course write:

    addM :: Maybe Int -> Maybe Int -> Maybe Int
    addM ma mb = do
      a <- ma
      b <- mb
      return (a + b)
As an aside, the inferred type would be much more general, because (+) works on any number and do/return work on any monad:

    addM :: (Monad m, Num a) => m a -> m a -> m a
Which means you could use it like this:

    addM (Just 5) (Just 10) == Just 15
But also like this:

    addM [1, 2] [4, 8]
      == [1 + 4, 1 + 8, 2 + 4, 2 + 8]
      == [5, 9, 6, 10]
Or like this:

    addM (Right 42) (Left "NaN") == Left "NaN"
And in many more interesting ways. :)



Of course, addA = liftA2 (+) is even more general.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: