首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用$ over示例

使用$ over示例
EN

Stack Overflow用户
提问于 2015-06-16 14:46:45
回答 1查看 106关注 0票数 1

看着liftA2

代码语言:javascript
复制
ghci> :t liftA2
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

我可以制作一个Either (a, a)

代码语言:javascript
复制
ghci> liftA2 (\x y -> (x, y)) (Right 100) (Right 1)
Right (100,1)

但是,如果使用$,则会出现编译时错误。

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-16 14:54:56

那是因为

代码语言:javascript
复制
liftA2 (\x y -> (x, y)) $ Right 100 $ Right 20

等于

代码语言:javascript
复制
liftA2 (\x y -> (x, y)) ( Right 100 ( Right 20 ))

您正在尝试将另一个参数插入Right构造函数(接受一个参数,但类型没有),而您的liftA2缺少一个。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30870960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档