为什么 Haskell 数字文字需要以数字开头和结尾?
在 Haskell 98 报告中,据说
浮点文字必须在小数点前后都包含数字;这可以确保小数点不会被误认为是点字符的另一种用法。
这可能还有什么其他用途?我无法想象任何这样的法律表达。
(为了澄清动机:我知道很多人在不需要的情况下一直写 9.0
或 0.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 技术交流群。
发布评论
评论(4)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
其他用途的一个示例是点运算符(或以点开头或结尾的任何其他运算符):
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 of9.0
, thus avoiding the need for.
altogether.