使用liftA2功能

时间:2016-10-16 04:26:28

标签: haskell

我想知道这是如何运作的。

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

x和g的类型(a - > Bool)如何对应liftA2的预期(f a)?

1 个答案:

答案 0 :(得分:4)

请记住,match: 1 match: 2 ((->) a)(也称为读者monad),因此Monad也是Applicative。取from the source for base

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

然后,liftA2 (||) x g(Num a, Eq a) => a -> Bool类型的函数,用于检查将参数应用于xg的结果中的任何一个是True

相关问题