[Rd] order of operations
@v|gro@@ @end|ng |rom ver|zon@net
Fri Aug 27 18:35:23 CEST 2021
Does anyone have a case where this construct has a valid use?
Didn't Python add a := operator recently that might be intended more for
such uses as compared to using the standard assignment operators? I wonder
if that has explicit guarantees of what happens in such cases, but that is
outside what this forum cares about. Just for the heck of it, I tried the
>>> (x := 1) * (x := 2)
Back to R, ...
The constructs can get arbitrarily complex as in:
(x <- (x <- 0) + 1) * (x <- (x <-2) + 1)
My impression is that when evaluation is left to right and also innermost
parentheses before outer ones, then something like the above goes in stages.
The first of two parenthetical expressions is evaluated first.
(x <- (x <- 0) + 1)
The inner parenthesis set x to zero then the outer one increments x to 1.
The full sub-expression evaluates to 1 and that value is set aside for a
But then the second parenthesis evaluates similarly, from inside out:
(x <- (x <-2) + 1)
It clearly resets x to 2 then increments it by 1 to 3 and returns a value of
3. That is multiplied by the first sub-expression to result in 3.
So for simple addition, even though it is commutative, is there any reason
any compiler or interpreter should not follow rules like the above?
Obviously with something like matrices, some operations are not abelian and
require more strict interpretation in the right order.
And note the expressions like the above can run into more complex quandaries
such as when you have a conditional with OR or AND parts that may be
short-circuited and in some cases, a variable you expected to be set, may
remain unset or ...
This reminds me a bit of languages that allow pre/post increment/decrement
operators like ++ and -- and questions about what order things happen.
Ideally, anything in which a deterministic order is not guaranteed should be
flagged by the language at compile time (or when interpreted) and refuse to
All I can say with computer languages and adding ever more features,
with greater power comes greater responsibility and often greater
From: R-devel <r-devel-bounces using r-project.org> On Behalf Of Gabor
Sent: Friday, August 27, 2021 11:32 AM
To: Thierry Onkelinx <thierry.onkelinx using inbo.be>
Cc: r-devel using r-project.org
Subject: Re: [Rd] order of operations
I agree and personally never do this but I would still like to know if it is
guaranteed behavior or not.
On Fri, Aug 27, 2021 at 11:28 AM Thierry Onkelinx <thierry.onkelinx using inbo.be>
> IMHO this is just bad practice. Whether the result is guaranteed or
> not, doesn't matter.
> ir. Thierry Onkelinx
> Statisticus / Statistician
> Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN
> BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie
> & Kwaliteitszorg / Team Biometrics & Quality Assurance
> thierry.onkelinx using inbo.be Havenlaan 88 bus 73, 1000 Brussel www.inbo.be
> ///////////////////// To call in the statistician after the experiment
> is done may be no more than asking him to perform a post-mortem
> examination: he may be able to say what the experiment died of. ~ Sir
> Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger
> Brinner The combination of some data and an aching desire for an
> answer does not ensure that a reasonable answer can be extracted from
> a given body of data.
> ~ John Tukey
> Op vr 27 aug. 2021 om 17:18 schreef Gabor Grothendieck <
> ggrothendieck using gmail.com>:
>> Are there any guarantees of whether x will equal 1 or 2 after this is
>> (x <- 1) * (x <- 2)
>> ##  2
>> ##  2
>> Statistics & Software Consulting
>> GKX Group, GKX Associates Inc.
>> tel: 1-877-GKX-GROUP
>> email: ggrothendieck at gmail.com
>> R-devel using r-project.org mailing list
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
email: ggrothendieck at gmail.com
[[alternative HTML version deleted]]
R-devel using r-project.org mailing list
More information about the R-devel