为什么'($ 4)(> 3)`等同于`4> 3`? [英] Why is `($ 4) (> 3)` equivalent to `4 > 3`?
问题描述
我注意到,今天我和Haskell一起玩,可以做一些类似于
($ 4)( > 3)
产生 True
。这里发生了什么?有一些直觉会很棒。
我的猜测是?它看起来像($ 4)
是一个不完整的函数应用程序,但我很困惑的是 $
是一个中缀运算符,所以不应该像(4 $)
?这不会编译,显然不是,这导致我相信我并不真正了解发生了什么。 (> 3)
这个术语对我来说很合理,因为如果你提供类似于(\ x - > x 4)(> 3)
,你最终得到相同的结果。
$ 4)
就是所谓的部分。这是部分应用中缀运算符的一种方式,但提供的是右侧而不是左侧。它与(flip($)4)
。
完全相同。(> 3)是一个节。
($ 4)(> 3)
可以被重写为
(flip($)4)(> 3)
与
相同flip($)4(> 3)
其中与
(> 3)$ 4
$ p相同$ p>
在这一点上,应该清楚的是,这可以归结为
(4> 3)
p>I noticed as I was playing around with Haskell today that it is possible to do something like
($ 4) (> 3)
which yields
True
. What is going on here? It'd be great to have some intuition.My guess? It looks like the
($ 4)
is an incomplete function application, but where I'm confused is that$
is an infix operator, so shouldn't it look like(4 $)
? This doesn't compile, so clearly not, which leads me to believe that I don't really understand what's going on. The(>3)
term makes sense to me, because if you supply something like(\x -> x 4) (>3)
, you end up with the same result.解决方案
($ 4)
is what's called a section. It's a way of partially applying an infix operator, but providing the right-hand side instead of the left. It's exactly equivalent to(flip ($) 4)
.Similarly, (> 3) is a section.
($ 4) (> 3)
can be rewritten as
(flip ($) 4) (> 3)
which is the same as
flip ($) 4 (> 3)
which is the same as
(> 3) $ 4
And at this point, it should be clear that this boils down to
(4 > 3)
.这篇关于为什么'($ 4)(> 3)`等同于`4> 3`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!