3 ms·
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
by evincarofautumn 14y ago
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. :)
- dllthomas 14y agoOf course, addA = liftA2 (+) is even more general.