[Rd] order of operations
Avi Gross
@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
example there:
>>> (x := 1) * (x := 2)
2
>>> 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
later multiplication.
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
go on.
All I can say with computer languages and adding ever more features,
with greater power comes greater responsibility and often greater
confusion.
-----Original Message-----
From: R-devel <r-devel-bounces using r-project.org> On Behalf Of Gabor
Grothendieck
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>
wrote:
> 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
>
> //////////////////////////////////////////////////////////////////////
> /////////////////////
>
> <https://www.inbo.be>
>
>
> 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
run?
>>
>> (x <- 1) * (x <- 2)
>> ## [1] 2
>> x
>> ## [1] 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
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
[[alternative HTML version deleted]]
______________________________________________
R-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list