<div dir="auto">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.<div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto">Let's have a look at it:</div><div dir="auto"><br></div><div dir="auto">fmap :: Functor f => (a -> b) -> f a -> f b</div><div dir="auto"><br></div><div dir="auto">Specialized:</div><div dir="auto"><br></div><div dir="auto">fmap ~ (a -> b) -> Maybe a -> Maybe b</div><div dir="auto"><br></div><div dir="auto">And concretize further:</div><div dir="auto"><br></div><div dir="auto">fmap ~ Num n => (n -> n) -> Maybe n -> Maybe n</div><div dir="auto"><br></div><div dir="auto">As you can see, given a transformation function and maybe some numeral, you'll get maybe another numeral.</div><div dir="auto"><br></div><div dir="auto">The implementation lools like this:</div><div dir="auto"><br></div><div dir="auto">fmap f (Just n) = Just (f n)</div><div dir="auto">fmap f Nothing = Nothing</div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto">You can see how the value cannot escape its container / context.</div><div dir="auto"><br></div><div dir="auto">Of course there are more complex such containers / context.</div><div dir="auto"><br></div><div dir="auto">Either represents a choice between two values. [] contains multiple values, IO contains (side-)effects, and so on.</div><div dir="auto"><br></div><div dir="auto">Hope this helps.</div><div dir="auto"><br></div><div dir="auto">nitrix</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, May 14, 2018, 08:18 David McBride <<a href="mailto:toad3k@gmail.com" target="_blank" rel="noreferrer">toad3k@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">let foo = \x -> Just (x + 1)<br><div>fmap foo (Just 9)</div><div><br></div>Just (Just 10)<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 14, 2018 at 8:15 AM, Olumide <span dir="ltr"><<a href="mailto:50295@web.de" rel="noreferrer noreferrer" target="_blank">50295@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear List,<br>
<br>
Chapter 14 of LYH appears to suggest that a Just value can be added to an Int. Quote from <a href="http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions" rel="noreferrer noreferrer noreferrer" target="_blank">http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions</a><br>
<br>
"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)."<br>
<br>
I've tried the following in ghci but got the error:<br>
<br>
<interactive>:12:1: error:<br>
    • Non type-variable argument in the constraint: Num (Maybe a)<br>
      (Use FlexibleContexts to permit this)<br>
    • When checking the inferred type<br>
        it :: forall a. (Num (Maybe a), Num a) => Maybe a<br>
<br>
<br>
Am I reading the quote wrong? Is Just (Just 10) a hypothetical?<br>
<br>
Regards,<br>
<br>
- Olumide<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" rel="noreferrer noreferrer" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" rel="noreferrer noreferrer" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>