<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style>
</head>
<body><div>Applicative is taking one step further, where the transformation itself is also in such container/context.<br></div>
<div><br></div>
<div><*> :: Functor => f (a -> b) -> f a -> f b<br></div>
<div><*> ~ Maybe (a -> b) -> Maybe a -> Maybe b<br></div>
<div><*> ~ Num n => Maybe (n -> n) -> Maybe n -> Maybe n<br></div>
<div><br></div>
<div>And the implementation:<br></div>
<div><br></div>
<div>(<*>) (Just f) (Just n) = Just (f n)<br></div>
<div>(<*>) Nothing (Just n) = Just n<br></div>
<div>(<*>) (Just f) Nothing = Nothing<br></div>
<div>(<*>) Nothing Nothing = Nothing<br></div>
<div><br></div>
<div>Thus, with everything we've learned, we should be able to deal with any situation thrown at use, using Functor or Applicative.<br></div>
<div> <br></div>
<div>Given (+1) and 42, I can basic function application with ($) or writing it (+1) 42.<br></div>
<div>Given (+1) and Just 42, I can use fmap to apply my transformation on that functor, fmap (+1) (Just 42).<br></div>
<div>Given Just (+1) and Just 42, I can use <*> to apply my transformation inside a functor (applicative functor) to another functor, Just (+1) <*> Just 42.<br></div>
<div>Given Just (+1) and 42, I can wrap 42 with Just and again use <*> as earlier, Just (+1) <*> Just 42.<br></div>
<div><br></div>
<div>Hope this helps.<br></div>
<div><br></div>
<div>nitrix<br></div>
<div><br></div>
<div>On Mon, May 14, 2018, at 9:17 AM, Alex Belanger wrote:<br></div>
<blockquote type="cite"><div><div>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.<br></div>
<div><br></div>
<div>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.<br></div>
<div><br></div>
<div>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.<br></div>
<div><br></div>
<div>Let's have a look at it:<br></div>
<div><br></div>
<div>fmap :: Functor f => (a -> b) -> f a -> f b<br></div>
<div><br></div>
<div>Specialized:<br></div>
<div><br></div>
<div>fmap ~ (a -> b) -> Maybe a -> Maybe b<br></div>
<div><br></div>
<div>And concretize further:<br></div>
<div><br></div>
<div>fmap ~ Num n => (n -> n) -> Maybe n -> Maybe n<br></div>
<div><br></div>
<div>As you can see, given a transformation function and maybe some numeral, you'll get maybe another numeral.<br></div>
<div><br></div>
<div>The implementation lools like this:<br></div>
<div><br></div>
<div>fmap f (Just n) = Just (f n)<br></div>
<div>fmap f Nothing = Nothing<br></div>
<div><br></div>
<div>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.<br></div>
<div><br></div>
<div>You can see how the value cannot escape its container / context.<br></div>
<div><br></div>
<div>Of course there are more complex such containers / context.<br></div>
<div><br></div>
<div>Either represents a choice between two values. [] contains multiple values, IO contains (side-)effects, and so on.<br></div>
<div><br></div>
<div>Hope this helps.<br></div>
<div><br></div>
<div>nitrix<br></div>
</div>
<div><br></div>
<div defang_data-gmailquote="yes"><div dir="ltr">On Mon, May 14, 2018, 08:18 David McBride <<a href="mailto:toad3k@gmail.com">toad3k@gmail.com</a>> wrote:<br></div>
<blockquote defang_data-gmailquote="yes" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204, 204, 204);padding-left:1ex;"><div dir="ltr"><div>let foo = \x -> Just (x + 1)<br></div>
<div>fmap foo (Just 9)<br></div>
<div><br></div>
<div>Just (Just 10)<br></div>
<div><br></div>
</div>
<div><div><br></div>
<div defang_data-gmailquote="yes"><div>On Mon, May 14, 2018 at 8:15 AM, Olumide <span dir="ltr"><<a href="mailto:50295@web.de">50295@web.de</a>></span> wrote:<br></div>
<blockquote defang_data-gmailquote="yes" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204, 204, 204);padding-left:1ex;"><div>Dear List,<br></div>
<div> <br></div>
<div> 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">http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions</a><br></div>
<div> <br></div>
<div> "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></div>
<div> <br></div>
<div> I've tried the following in ghci but got the error:<br></div>
<div> <br></div>
<div> <interactive>:12:1: error:<br></div>
<div>     • Non type-variable argument in the constraint: Num (Maybe a)<br></div>
<div>       (Use FlexibleContexts to permit this)<br></div>
<div>     • When checking the inferred type<br></div>
<div>         it :: forall a. (Num (Maybe a), Num a) => Maybe a<br></div>
<div> <br></div>
<div> <br></div>
<div> Am I reading the quote wrong? Is Just (Just 10) a hypothetical?<br></div>
<div> <br></div>
<div> Regards,<br></div>
<div> <br></div>
<div> - Olumide<br></div>
<div> _______________________________________________<br></div>
<div> Beginners mailing list<br></div>
<div> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br></div>
<div> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br></div>
</blockquote></div>
<div><br></div>
</div>
<div>_______________________________________________<br></div>
<div> Beginners mailing list<br></div>
<div> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br></div>
<div> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br></div>
</blockquote></div>
</blockquote><div><br></div>
</body>
</html>