是否可以在“flip”中使用一元函数而不是二进制函数?
Prelude
函数flip
的类型是:
flip :: (a -> b -> c) -> b -> a -> c
即,它需要一个二元函数和两个参数。
Prelude
函数id
的类型是:
id :: a -> a
但是flip id
的类型是:
flip id :: a -> (a -> b) -> b
如何应用flipid
是一元函数并且 flip
需要第一个参数的二进制函数时,code> 到 id
?
顺便提一句。 flip id
类似于\ xf -> fx
The type of the Prelude
function flip
is:
flip :: (a -> b -> c) -> b -> a -> c
I.e., it takes one binary function and two arguments.
The type of the Prelude
function id
is:
id :: a -> a
But the type of flip id
is:
flip id :: a -> (a -> b) -> b
How is it possible to apply flip
to id
when id
is a unary function and flip
requires binary function for the first arg?
btw. flip id
is similar to \ x f -> f x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Haskell 通过设置
a = b ->; 使
.因此:id
适合flip
第一个参数的类型。 c其中
id
被视为相当于
仅适用于一元函数的
id
特化类型。编辑:我想我可以将第一行改写为:
Haskell 推断
id
适合flip
ifa = b -> 第一个参数的类型; c
。万一更清楚了。
Haskell makes
id
fit the type of the first argument toflip
by settinga = b -> c
. So:where
id
is taken to be of typewhich is equivalent to
i.e. a specialisation of
id
that only applies to unary functions.Edit: I think I might rephrase my first line as:
Haskell deduces that
id
fits the type of the first argument toflip
ifa = b -> c
.In case that's any clearer.
Nefrubyr 解释得很好。
另一种(希望)使其更加直观的方法是考虑函数应用运算符
($)
。($)
是id
的一种特殊形式:我已经看到了定义
(#) = Flip ($)
,这样你就可以将参数写在应用于的函数之前:obj # show
。显然,由于
($)
只是id
的一种特殊形式,因此您也可以编写:(#) = Flip id
Nefrubyr explains it very well.
Another way to (hopefully) make this a bit more intuitive is to think of the function application operator
($)
.($)
is a specialized form ofid
:I've seen the definition
(#) = flip ($)
, such that you can write the argument before the function its applied to:obj # show
.Obviously, since
($)
is just a specialized form ofid
, you could also write:(#) = flip id