Skip to content
Prev 12206 / 398502 Next

a < b < c is alway TRUE

Duncan Murdoch <dmurdoch at pair.com> writes:
(S is not *that* old (New S, 1988), but it does build on principles of
parser construction that are a decade or more older.)

The general principle is that rules should be clear and consistent!
You don't get anywhere by assuming that computer scientists don't know
what they are doing and trying to change rules to suite a naive
audience. Hardly any programming language allows the 1 < x < 2
construct. Either they throw an error or they do something unexpected
(but logical) due to type coercion. (Also, I don't think any versions
of *formal* mathematical logic would allow that notation, relational
operators are binary.) 

Whenever you try to make a language do something "intuitive" you also
find gotchas lurking inside, and of a different sort than what you
could figure out by considering the rules as they are, instead of what
you expect them to be.

Consider the following:

x <- rep(NA,10)
x < 0

This will work fine and the result of the comparison is a vector of
NA's. However, NA is a logical constant, so if you tried to prevent
comparisons of logicals to numericals, it would bomb. These all-NA
vectors can creep in all over the place, so you'd get to pepper large
portions of your code with checks like

if (is.logical(x) && all(is.na(x))) rep(NA,length(x)) else x < 0

(There have been countless of these isues coming up over the years,
and most often, once the full implications of a change of semantics
has been realized, they have ended with a "perhaps Uncle John was
right after all"...)