3

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?

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • 1
    You give the best example yourself why numeric literals should not end with or start with a '.'. Your proposed "solution" sounds like a bad hack to me - at least it destroys orthogonality (i.e. why can I write `abs 3+b` but not `max 3.b` when I mean `(.) (max 3) b` – Ingo Oct 03 '11 at 23:33
  • 1
    @Ingo for the record, that example was taken from Rotsor's answer. — As for orthogonality: you can't do `max A.b` either (with `A` a data constructor of an `Ord` instance, so that `(.) (max A) b` is legal). – leftaroundabout Oct 04 '11 at 00:22

4 Answers4

5

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.

Rotsor
  • 13,655
  • 6
  • 43
  • 57
  • Indeed, being able to write "integer" literals for any `Num` instance is one of the great things about Haskell. As for ranges, these seem unambiguous, don't they? At least, `[1.0 0.10]` is illegal. – leftaroundabout Oct 03 '11 at 23:10
  • @leftaroundabout Oh, but that's where you're wrong! Try it out in lambdabot: it evaluates to `1.0`. All you need is a function instance of `Num`. – Daniel Wagner Oct 03 '11 at 23:47
3

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.

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
3

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.

augustss
  • 22,884
  • 5
  • 56
  • 93
2

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.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • 3
    That's an argument insofar as people are _used_ to the surrounding-zeroes-notation – _if_ they are. In computer science probably yes, but in science and engineering a trailing zero carries the additional information of accuracy, so for us it's rather confusing and _reduces_ readability. — I can't argue about leaving away _leading_ zeroes, which is more of a mannerism of mine that I carry around since my QBasic times, almost everyone tells me to stop it. – leftaroundabout Oct 04 '11 at 00:43