2015-06-16 34 views
1

看着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 

爲什麼我不能使用$這個例子來獲得th與括號相同的結果?

+9

因爲'$'是右關聯的。 '右邊100(右邊20)'不檢查。 – Mephy

+8

僅供參考,您可以使用'(,)'代替lambda:'liftA2(,)(Right 100)(Right 1)' – Lee

回答

12

那是因爲

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

等於

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

您正在試圖推一個額外的參數到Right構造函數(接受一個參數,但類型現在沒有)和您liftA2缺少​​一個。