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與括號相同的結果?
因爲'$'是右關聯的。 '右邊100(右邊20)'不檢查。 – Mephy
僅供參考,您可以使用'(,)'代替lambda:'liftA2(,)(Right 100)(Right 1)' – Lee