(r ->) applicative functor

I have some problems understanding how the application (->) rworks in haskell.

For example, if I have

(+) <$> (+3) <*> (*100) $ 5

I know that you got the result 508, I understand that you accept the result (5 + 3)and (5 * 100), and you apply the function (+)to both.

However, I do not quite understand what is going on, I assume that the paranthesized expression looks like this:

((+) <$> (+3)) <*> (*100)

From my understanding of what happens, is that your mapping is (+)over the final result (+3), and then you use the operator <*>to apply this function to the final result(*100)

However, I do not understand the implementation <*>for the instance (->) rand why I cannot write:

(+3) <*> (*100)

<*>, <$>, (->) r?

+4
5

<$> - fmap, (->) r (.) ( ):

intance Functor ((->) r) where
  fmap f g = f . g

<*>, :

instance Applicative ((->) r) where
  (<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)
  f <*> g = \x -> f x (g x)

r a b r a. funtion r b. , , :

\x ->

f, , b:

\x -> f _ _

f r a. r x ( r, a, g x:

\x -> f x (g x)

, . Haskell Prelude.

+7

<*>:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

-, $:

($) :: (a -> b) -> a -> b

, ! , <*> , . Applicative, Identity:

ghci> Identity (+) <*> Identity 1 <*> Identity 2
Identity 3

, Maybe:

ghci> Just (+) <*> Just 1 <*> Just 2
Just 3
ghci> Just (+) <*> Nothing <*> Just 2
Nothing

(->) r Applicative , , "" :

ghci> ((\_ -> (+)) <*> (+ 3) <*> (* 100)) 5
508

<*>, Ive (+). , Applicative typeclass pure, , "" :

ghci> (pure (+) <*> (+ 3) <*> (* 100)) 5
508

, , pure x <*> y, x <$> y Applicative, <$> infix fmap. :

ghci> ((+) <$> (+ 3) <*> (* 100)) 5
508

, , :

f <$> a <*> b

... - f a b, Applicative . , Applicative " ", :

(| f a b |)

Haskellers, , -, , <$> <*> .

+5

Haskell, ,

<$> , .

:

(+) <$> (+3)

:

fmap (+) (+3)

Functor (- > ) r, :

fmap f g = (\x -> f (g x))

, fmap (+) (+3) (\x -> (+) (x + 3))

, a -> (a -> a)

! (+) <$> (+3) <*>!

, ? <*>:

f (a -> b) -> f a -> f b 

, a -> (a -> a)

, <*>, :

f <*> g = (\x -> f x (g x))

, , :

(+) <$> (+3) <*> (+5)
(\x -> (+) (x + 3)) <*> (+5)
(\y -> (\x -> (+) (x + 3)) y (y + 5))
(\y -> (+) (y + 3) (y + 5))
+4

( , ):

(<$>) :: (a -> b) -> (r -> a) -> r -> b
f <$> g = \x -> f (g x)

(<*>) :: (r -> a -> b) -> (r -> a) -> r -> b
f <*> g = \x -> f x (g x)

<$> - . (<$>) = (.).

. - f :: r -> a -> b, b. x :: r f, - a - g :: r -> a x :: r.


, <*> S SKI, pure (-> r) K :: a -> b -> a ().

+3

(->) e Functor Applicative, , . (->) e "" Reader e.

newtype Reader e a = Reader
  { runReader :: e -> a }

e "". Reader e a ", a e".

Reader e a, :

instance Functor (Reader e) where
  fmap f r = Reader $ \e -> f (runReader r e)

, .

instance Applicative (Reader e) where
  -- Produce a value without using the environment
  pure a = Reader $ \ _e -> a

  -- Produce a function and a value using the same environment;
  -- apply the function to the value

  rf <*> rx = Reader $ \e -> (runReader rf e) (runReader rx e)

You can use the usual arguments Applicativefor this like any other application functor.

+3
source

Source: https://habr.com/ru/post/1650238/


All Articles