Haskell $ 运算符是否存在逆操作?
一个简单的问题是,Haskell 中是否有一个运算符,其工作方式类似于美元符号,但优先考虑左侧。 IE 而不是
f (x 1)
纯粹
f $ x 1
像我想的那样写成这
x 1 $ f
是一种风格问题。我正在按顺序运行一系列函数,如果我可以从左到右编写它们以匹配我从左到右读取的函数,那就太好了。如果有这样的运营商吗?
[更新] 有几个人问我是否无法定义自己的。作为回答,我想在重新发明轮子之前检查是否存在现有的操作员。
A quick question, is there an operator in Haskell that works like the dollar sign but gives precedence to the left hand side. I.E. instead of
f (x 1)
being written as
f $ x 1
I'd like to write it as
x 1 $ f
This is purely a stylistic thing. I'm running a sequence of functions in order and it would be nice if I could write them left to right to match that I read left to right. If there an operator for this?
[update] A couple of people have asked if I can't define my own. In answer, I wanted to check there wasn't an existing operator before I reinvented the wheel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 GHC 7.10 (
base
4.8.0.0) 开始,&
位于Data.Function
中:https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Function.htmlAs of GHC 7.10 (
base
4.8.0.0),&
is inData.Function
: https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Function.html在 Haskell 中,您可以使用
flip
更改任何二元函数或运算符的参数顺序:In Haskell you can use
flip
to change arguments' order of any binary function or operator:我不知道是否有标准的运算符,但是什么阻止你编写自己的运算符呢?这在 ghci 中有效:
更新:在 hoogle 上搜索
a ->; (a→b)→ b
(它是这个运算符的类型)没有发现任何有用的东西。I do not know, whether there is an standart operator, but what prevents you from writing your own? This works in ghci:
UPDATE: searching on hoogle for
a -> (a -> b) -> b
(it is the type of this operator) found nothing useful.这个组合器是在 data-aviary 包中定义的(开玩笑):
尽管实际使用该包是相当困难的做愚蠢的事情,阅读源代码很有趣,并揭示了 这个组合器是通过
flip id
(或者用鸟类学的说法,cardinal idiot
)的魔法咒语形成的。This combinator is defined (tongue in cheek) in the data-aviary package:
Although actually using that package is a rather silly thing to do, reading the source is fun, and reveals that this combinator is formed via the magic incantation of
flip id
(or, in ornithological parlance,cardinal idiot
).我不知道有任何标准版本,但我在几个地方看到 (#) 用于此目的。我特别想到的是 HOC,它以这样的习惯用法使用它:
我似乎记得看到其他“面向对象”库以相同的方式使用 # 运算符,但不记得有多少个或哪些。
I am not aware of any standard version, but I've seen (#) used for that purpose in a couple places. The one in particular that comes to mind is HOC, which uses it in an idiom like:
I seem to recall seeing other "object-oriented" libraries using the # operator in the same way, but cannot remember how many or which ones.
你不能重新定义
$
吗?或者只是选择不同的运算符,例如
$$
Can't you just redefine
$
.Or just choose a different operator, like
$$