[R] Can we get rid of && and ||?
Bill.Venables at cmis.csiro.au
Thu Apr 13 14:16:52 CEST 2000
At 12:24 PM 13/4/2000 +0200, GB wrote:
>I have some doubts about the implementation of the operators '&&' and
>'||'. I only discuss '&&' here. The reason: One of my students tried
>(slightly simplified here)
>> x <- c(0.5, 1.2, -0.8, 0.7)
>> y <- x[(x > 0) && (x < 1)]
> 0.5 1.2 -0.8 0.7
Notice that (x > 0) && (x < 1) is a scalar logical (here TRUE). The vector is
reproduced because of the recycling rule.
>This was not his intention. He wanted
>> y <- x[(x > 0) & (x < 1)]
> 0.5 0.7
..my guess is he learned something pretty thoroughly, then!
>I suggested that his first attempt should render a "syntax error", since
>'&&' only applies to scalar logicals. However, upon reading
>the documentation, I found that 'a && b' is in fact equivalent to
>'a & b' (with one exception).
>Therefore, '&&' seems to be of little use in R programming. You can
>almost always use '&' instead (This is in sharp contrast to the use of
>& and && in the C programming language). I can only see one
>advantage (admittedly important, though) with '&&'. In
>if (is.numeric(x) && min(x) > 0) ..... (Found in MASS3, p. 94)
>you avoid 'min(x)' to be evaluated if x is non-numeric. This doesn't
>work with '&', because then both expressions are evaluated before the
This looks like a "go with the flow" argument: if the user expects something
to work that way, make it do so. I have to disagree, very strongly.
>Provided there are no other differences, I suggest that either
>a) '&&' is made obsolete, and '&' is 'improved' so that b in
> 'a & b' is evaluated only if some component of a is TRUE, or
I think you miss the point quite seriously. Under the present semantics
in 'a & b' *both* a and b are *guaranteed* unconditionally to be evaluated,
and in a lazy evaluation language, especially, that unconditional evaluation
can be vital. With 'a && b' (a) the result is guaranteed to be scalar and
(b) b is only *conditionally* evaluated (as you know, of course, but I must
emphasize it nevertheless).
Viewed in this light 'a & b' and 'a && b' are both syntactically and
semantically quite different. Programmers should get to realise that,
after which you start to read code with a better understanding of the
implications. If you blurred the distinction between & and && you would
make code less readible (to the informed reader, at least) and make the
interpreter more complex for no good purpose.
>b) a warning (or syntax error) is given if '&&' is used with at least
> one non-scalar argument.
Well it cannot be a syntax error since the properties of the arguments,
a and b, are unknown at parse time (and may be different on different
A warning would be useful, though, and in line with what happens "elsewhere".
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help