为什么 Haskell 数字文字需要以数字开头和结尾?

发布于 12-08 01:56 字数 874 浏览 2 评论 0原文

Haskell 98 报告中,据说

浮点文字必须在小数点前后都包含数字;这可以确保小数点不会被误认为是点字符的另一种用法。

这可能还有什么其他用途?我无法想象任何这样的法律表达。

(为了澄清动机:我知道很多人在不需要的情况下一直写 9.00.7 这样的数字,但我可以我对此不太友好,我同意 0.7 而不是更紧凑但没有更好的 .7,但写出的尾随零对我来说感觉不对,除非他们表示某些数量精确到十分之一,在 Haskell 让我写 9.0-数字的情况下很少出现这种情况。)


I forgot it's legal to write function composition without surrounding whitespaces! That's of course a possibility, though one could avoid this problem by parsing floating literals greedily, such that replicate 3 . pred$8((replicate 3) . pred) 8 but replicate 3.pred$8(replicate 3.0 pred)8.


没有任何表达式需要整数文字直接位于 . 旁边,并且没有空格?

In The Haskell 98 Report it's said that

A floating literal must contain digits both before and after the decimal point; this ensures that a decimal point cannot be mistaken for another use of the dot character.

What other use might this be? I can't imagine any such legal expression.

(To clarify the motivation: I'm aware that many people write numbers like 9.0 or 0.7 all the time without needing to, but I can't quite befriend myself with this. I'm ok with 0.7 rather then the more compact but otherwise no better .7, but outwritten trailing zeroes feel just wrong to me unless they express some quantity is precise up to tenths, which is seldom the case in the occasions Haskell makes me write 9.0-numbers.)


I forgot it's legal to write function composition without surrounding whitespaces! That's of course a possibility, though one could avoid this problem by parsing floating literals greedily, such that replicate 3 . pred$8((replicate 3) . pred) 8 but replicate 3.pred$8(replicate 3.0 pred)8.

There is no expression where an integer literal is required to stand directly next to a ., without whitespace?

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

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

发布评论

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

评论(4

小鸟爱天空丶2024-12-15 01:56:05

其他用途的一个示例是点运算符(或以点开头或结尾的任何其他运算符):replicate 3.pred$8

另一种可能的用途是在范围表达式中:[1..10]

此外,您(几乎)始终可以编写 9 而不是 9.0,从而完全不需要 .

One example of other uses is a dot operator (or any other operator starting or ending with a dot): replicate 3.pred$8.

Another possible use is in range expressions: [1..10].

Also, you can (almost) always write 9 instead of 9.0, thus avoiding the need for . altogether.

深海里的那抹蓝2024-12-15 01:56:05

(.) 最突出的用法之一是函数组合。所以 haskell 编译器解释 a 。 1 用一个数字组成函数a,但不知道要做什么;反过来也类似。 (.) 的其他用法可以在此处找到。

我不知道 .70.7 的其他问题。

One of the most prominent usages of (.) is the function composition. And so the haskell compiler interpretes a . 1 composing the function a with a number and does not know what to do; analogously the other way 'round. Other usage of (.) could be found here.

Other problems with .7 vs. 0.7 are not known to me.

物价感观2024-12-15 01:56:05

我觉得允许“9”确实没什么问题。和“.7”。我认为现在的设计更多地体现了Haskell最初设计者的想法。

I don't really seem much of a problem with allowing '9.' and '.7'. I think the current design is more of a reflection of the ideas of the original designers of Haskell.

七月上2024-12-15 01:56:05

虽然它可能可以消除歧义,但我认为允许 .77. 不会有太多好处。代码是供人和机器阅读的,并且在文字的两端比在中间更容易意外地错过小数点。

任何一天我都会在保存的字节上获得额外的可读性。

While it could probably be disambiguated, I don't think there is much to be gained from allowing .7 and 7.. Code is meant to be read by people as well as machines, and it's much easier to accidentally miss a decimal point at either end of a literal than in the middle.

I'll take the extra readability over the saved byte any day.

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