Haskell $ 运算符是否存在逆操作?

发布于 2024-09-30 11:35:14 字数 322 浏览 6 评论 0原文

一个简单的问题是,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

人海汹涌 2024-10-07 11:35:14

从 GHC 7.10 (base 4.8.0.0) 开始,& 位于 Data.Function 中:https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Function.html

As of GHC 7.10 (base 4.8.0.0), & is in Data.Function: https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Function.html

甜心小果奶 2024-10-07 11:35:14

在 Haskell 中,您可以使用 flip 更改任何二元函数或运算符的参数顺序:

ghci> let (|>) = flip ($)
ghci> 3 |> (+4) |> (*6)
42

In Haskell you can use flip to change arguments' order of any binary function or operator:

ghci> let (|>) = flip ($)
ghci> 3 |> (+4) |> (*6)
42
苄①跕圉湢 2024-10-07 11:35:14

我不知道是否有标准的运算符,但是什么阻止你编写自己的运算符呢?这在 ghci 中有效:

Prelude> let a 
gt; b = b a
Prelude> 1 
gt; (+2)
3
Prelude> sum [1, 2] 
gt; (+2)
5
Prelude> map (+2) [1, 2] 
gt; map (+3)
[6,7]

更新:在 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:

Prelude> let a 
gt; b = b a
Prelude> 1 
gt; (+2)
3
Prelude> sum [1, 2] 
gt; (+2)
5
Prelude> map (+2) [1, 2] 
gt; map (+3)
[6,7]

UPDATE: searching on hoogle for a -> (a -> b) -> b (it is the type of this operator) found nothing useful.

爱要勇敢去追 2024-10-07 11:35:14

这个组合器是在 data-aviary 包中定义的(开玩笑):

Prelude Data.Aviary.BirdsInter> 1 `thrush` (+2)
Loading package data-aviary-0.2.3 ... linking ... done.
3

尽管实际使用该包是相当困难的做愚蠢的事情,阅读源代码很有趣,并揭示了 这个组合器是通过flip id(或者用鸟类学的说法,cardinal idiot)的魔法咒语形成的。

This combinator is defined (tongue in cheek) in the data-aviary package:

Prelude Data.Aviary.BirdsInter> 1 `thrush` (+2)
Loading package data-aviary-0.2.3 ... linking ... done.
3

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).

甜味超标? 2024-10-07 11:35:14

我不知道有任何标准版本,但我在几个地方看到 (#) 用于此目的。我特别想到的是 HOC,它以这样的习惯用法使用它:

someObject # someMessage param1 param2

我似乎记得看到其他“面向对象”库以相同的方式使用 # 运算符,但不记得有多少个或哪些。

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:

someObject # someMessage param1 param2

I seem to recall seeing other "object-oriented" libraries using the # operator in the same way, but cannot remember how many or which ones.

挽心 2024-10-07 11:35:14

你不能重新定义$吗?

let ($) x f = f x

或者只是选择不同的运算符,例如 $$

Can't you just redefine $.

let ($) x f = f x

Or just choose a different operator, like $$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文