Haskell 中一元运算符的前缀形式

发布于 2024-09-12 15:46:28 字数 427 浏览 2 评论 0原文

在 GHCi 中:

  1. 前奏> (+3) 2
    5
  2. 前奏> (*3) 2
    6
  3. 前奏> (/3) 2
    0.6666666666666666
  4. 前奏> (-3) 2
    没有 (Num (t -> t1)) 的实例
    :1:2
    处的文字 3' 产生 可能的修复:为 (Num (t -> t1)) 添加实例声明
    表达式中:3
    表达式中: (- 3) 2
    it' 的定义中:it = (- 3) 2

如何更正最后一个使其返回-1?

In GHCi:

  1. Prelude> (+3) 2
    5
  2. Prelude> (*3) 2
    6
  3. Prelude> (/3) 2
    0.6666666666666666
  4. Prelude> (-3) 2
    No instance for (Num (t -> t1))
    arising from the literal 3' at <interactive>:1:2
    Possible fix: add an instance declaration for (Num (t -> t1))
    In the expression: 3
    In the expression: (- 3) 2
    In the definition of
    it': it = (- 3) 2

How can I correct the last one to make it return -1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

平安喜乐 2024-09-19 15:46:28

Haskell 的语法不允许你像这样使用 - 。请改用 subtract 函数:

(subtract 3) 2

Haskell's grammar doesn't allow you to use - like that. Use the subtract function instead:

(subtract 3) 2
黯淡〆 2024-09-19 15:46:28

作为 grddev 的答案的脚注,这里是相关的Haskell 98 报告中的段落:

特殊形式-e表示前缀
否定,唯一的前缀运算符
Haskell,and 是 negate 的语法
(e)
。二元 - 运算符不
必然参考定义
前奏中的-;可能会被模块系统反弹。但是,一元 -
将始终引用否定
Prelude 中定义的函数。那里
本地意义之间没有联系
- 运算符和一元否定。

当我第一次遇到它时,这让我感到沮丧:我无法理解为什么当 :info (+):info (-)< 时运算符在这种情况下的行为如此不同/code> 看起来基本相同。

您可以使用 subtract,正如 grddev 所建议的那样,或者您也可以定义一个新的中缀运算符:

Prelude> let (#) = (-)
Prelude> (# 3) 2
-1

subtract 的优点是其他可能阅读您代码的人会很熟悉。

As a footnote to grddev's answer, here's the relevant paragraph from the Haskell 98 Report:

The special form -e denotes prefix
negation, the only prefix operator in
Haskell, and is syntax for negate
(e)
. The binary - operator does not
necessarily refer to the definition of
- in the Prelude; it may be rebound by the module system. However, unary -
will always refer to the negate
function defined in the Prelude. There
is no link between the local meaning
of the - operator and unary negation.

This is something that frustrated me when I first came across it: I couldn't understand why the operators behaved so differently in this context when :info (+) and :info (-) looked basically identical.

You could use subtract, as grddev suggests, or you could just define a new infix operator:

Prelude> let (#) = (-)
Prelude> (# 3) 2
-1

subtract has the advantage of being familiar to other people who might read your code.

寻找一个思念的角度 2024-09-19 15:46:28

你可以这样做,

(-) 3 2

但这会给你 1。要得到 -1,你需要将 3 绑定到 - 的第二个参数,你可以使用

flip (-) 3 2

You can do

(-) 3 2

but that will give you 1. To have -1, you need to bind the 3 to the second argument of -, which you can do using

flip (-) 3 2
菩提树下叶撕阳。 2024-09-19 15:46:28

如果你想保持原来的形状,你可以随时添加负数:

(+ -3)

它不漂亮,但它更适合你的图案。

If you're intent on keeping your original shape, you can always add the negative:

(+ -3)

It ain't pretty, but it fits your pattern a little bit more.

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