在括号上使用$示例

时间:2015-06-16 14:46:45

标签: haskell

查看liftA2

ghci> :t liftA2
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

我可以制作Either (a, a)

ghci> liftA2 (\x y -> (x, y)) (Right 100) (Right 1)
Right (100,1)

但如果我使用$

,我会收到编译时错误
ghci> liftA2 (\x y -> (x, y)) $ Right 100 $ Right 20

<interactive>:23:27:
    Couldn't match expected type `Either a1 b1 -> f t'
                with actual type `Either a0 b0'
    Relevant bindings include
      it :: f t1 -> f (t, t1) (bound at <interactive>:23:1)
    The first argument of ($) takes one argument,
    but its type `Either a0 b0' has none
    In the second argument of `($)', namely `Right 100 $ Right 20'
    In the expression: liftA2 (\ x y -> (x, y)) $ Right 100 $ Right 20

为什么我不能在此示例中使用$来获得与括号相同的结果?

1 个答案:

答案 0 :(得分:12)

那是因为

$i

等于

liftA2 (\x y -> (x, y)) $ Right 100 $ Right 20

您正试图将另一个参数推入liftA2 (\x y -> (x, y)) ( Right 100 ( Right 20 )) 构造函数(接受一个参数,但类型没有),而您的liftA2缺少​​一个。