看着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为什么不能在这个示例中使用$来获得与圆括号相同的结果?
发布于 2015-06-16 14:54:56
那是因为
liftA2 (\x y -> (x, y)) $ Right 100 $ Right 20等于
liftA2 (\x y -> (x, y)) ( Right 100 ( Right 20 ))您正在尝试将另一个参数插入Right构造函数(接受一个参数,但类型没有),而您的liftA2缺少一个。
https://stackoverflow.com/questions/30870960
复制相似问题