Adding 1 to Just 9

Dear List, Chapter 14 of LYH appears to suggest that a Just value can be added to an Int. Quote from http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions "For instance, say we have Just 9 and the function \x -> Just (x+1). If we map this function over Just 9, we're left with Just (Just 10)." I've tried the following in ghci but got the error: <interactive>:12:1: error: • Non type-variable argument in the constraint: Num (Maybe a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num (Maybe a), Num a) => Maybe a Am I reading the quote wrong? Is Just (Just 10) a hypothetical? Regards, - Olumide

let foo = \x -> Just (x + 1) fmap foo (Just 9) Just (Just 10) On Mon, May 14, 2018 at 8:15 AM, Olumide <50295@web.de> wrote:
Dear List,
Chapter 14 of LYH appears to suggest that a Just value can be added to an Int. Quote from http://learnyouahaskell.com/fo r-a-few-monads-more#useful-monadic-functions
"For instance, say we have Just 9 and the function \x -> Just (x+1). If we map this function over Just 9, we're left with Just (Just 10)."
I've tried the following in ghci but got the error:
<interactive>:12:1: error: • Non type-variable argument in the constraint: Num (Maybe a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num (Maybe a), Num a) => Maybe a
Am I reading the quote wrong? Is Just (Just 10) a hypothetical?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

A first approximative intuition is to think of Functors as containers (or
more like a context) and of fmap as a way to apply a transformation
function of your choice on the contained value, respecting the
signification of that context.
For example, Maybe represents the possibility of having or not having a
value, therefore, fmap will apply your transformation on that value if it
exists, otherwise you're still left with nothing.
This example might seem straight forward but it had to be defined
somewhere, thus, made possible by the Functor instance implementation for
the type Maybe.
Let's have a look at it:
fmap :: Functor f => (a -> b) -> f a -> f b
Specialized:
fmap ~ (a -> b) -> Maybe a -> Maybe b
And concretize further:
fmap ~ Num n => (n -> n) -> Maybe n -> Maybe n
As you can see, given a transformation function and maybe some numeral,
you'll get maybe another numeral.
The implementation lools like this:
fmap f (Just n) = Just (f n)
fmap f Nothing = Nothing
Thus, starting with Nothing, we cannot apply our tranformation so you stay
with Nothing. Similarly, with Just n, we're able to pattern match to obtain
that n, apply our transformation f on that n, and then rewrap everything
back into Just.
You can see how the value cannot escape its container / context.
Of course there are more complex such containers / context.
Either represents a choice between two values. [] contains multiple values,
IO contains (side-)effects, and so on.
Hope this helps.
nitrix
On Mon, May 14, 2018, 08:18 David McBride
let foo = \x -> Just (x + 1) fmap foo (Just 9)
Just (Just 10)
On Mon, May 14, 2018 at 8:15 AM, Olumide <50295@web.de> wrote:
Dear List,
Chapter 14 of LYH appears to suggest that a Just value can be added to an Int. Quote from http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions
"For instance, say we have Just 9 and the function \x -> Just (x+1). If we map this function over Just 9, we're left with Just (Just 10)."
I've tried the following in ghci but got the error:
<interactive>:12:1: error: • Non type-variable argument in the constraint: Num (Maybe a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num (Maybe a), Num a) => Maybe a
Am I reading the quote wrong? Is Just (Just 10) a hypothetical?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Alex Belanger
-
David McBride
-
Olumide