内容简介:翻译自:https://stackoverflow.com/questions/26073878/why-it-is-impossible-to-multiply-negative-numbers-in-haskell-without-brackets
在haskell gchi中乘以5 * -3给我和错误.但乘以5 *( – 3)可以正常工作.为什么需要括号?
$ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> 5 * -3
<interactive>:2:1:
Precedence parsing error
cannot mix `*' [infixl 7] and prefix `-' [infixl 6] in the same infix expression
Prelude> 5 * (-3)
-15
Prelude>
因为在Haskell中如何指定 unary minus
运算符.不幸的是,它有点神奇.这是语言规范的一个奇怪的角落.
一元减号是该语言中唯一的一元运算符.支持一元减法语法只是为了支持否定函数的语法糖.
一方面,这让我们–7与-7相同.另一方面,它打破了其他的东西,比如节语法.
正如您所看到的,在编写混合固定表达式时会造成混淆.
您需要使用negate括起来,如下所示:
5 * (-3)
翻译自:https://stackoverflow.com/questions/26073878/why-it-is-impossible-to-multiply-negative-numbers-in-haskell-without-brackets
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Four
Scott Galloway / Portfolio / 2017-10-3 / USD 28.00
NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!