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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
let foo = \x -> Just (x + 1) fmap foo (Just 9) On Mon, May 14, 2018 at 8:15 AM, Olumide <[hidden email]> wrote: Dear List, _______________________________________________ Beginners mailing list [hidden email] 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 <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Applicative is taking one step further, where the transformation itself is also in such container/context.
<*> :: Functor => f (a -> b) -> f a -> f b
<*> ~ Maybe (a -> b) -> Maybe a -> Maybe b
<*> ~ Num n => Maybe (n -> n) -> Maybe n -> Maybe n
And the implementation:
(<*>) (Just f) (Just n) = Just (f n)
(<*>) Nothing (Just n) = Just n
(<*>) (Just f) Nothing = Nothing
(<*>) Nothing Nothing = Nothing
Thus, with everything we've learned, we should be able to deal with any situation thrown at use, using Functor or Applicative.
Given (+1) and 42, I can basic function application with ($) or writing it (+1) 42.
Given (+1) and Just 42, I can use fmap to apply my transformation on that functor, fmap (+1) (Just 42).
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.
Given Just (+1) and 42, I can wrap 42 with Just and again use <*> as earlier, Just (+1) <*> Just 42.
Hope this helps.
nitrix
On Mon, May 14, 2018, at 9:17 AM, Alex Belanger wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |