简体   繁体   中英

Using liftA2 with functions

I am wondering how this works.

x 9001 = True
x _ = False

g 42 = True
g _ = False

(liftA2 (||) x g) 42 = True

liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
x :: (Eq a, Num a) => a -> Bool
g :: (Eq a, Num a) => a -> Bool

How does the type of x and g (a -> Bool) correspond to what liftA2 expects (fa)?

Remember that ((->) a) is a Monad (also known as the reader monad), and hence an Applicative too. Taken from the source for base

instance Applicative ((->) a) where
  pure = const
  (<*>) f g x = f x (g x)

Then, liftA2 (||) xg is the function of type (Num a, Eq a) => a -> Bool that checks if either of the results of applying the argument to x and g is True .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM