Why do Haskell numerical literals need to start and end with digits?

前端 未结 4 795
误落风尘
误落风尘 2020-12-21 09:54

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 p

相关标签:
4条回答
  • 2020-12-21 10:15

    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.

    0 讨论(0)
  • 2020-12-21 10:22

    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.

    0 讨论(0)
  • 2020-12-21 10:22

    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.

    0 讨论(0)
  • 2020-12-21 10:40

    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.

    0 讨论(0)
提交回复
热议问题