[Rd] order of operations

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat Aug 28 10:48:51 CEST 2021


On 28/08/2021 3:44 a.m., GILLIBERT, Andre wrote:
> For the discussion:
> 
>  > Yes, and were it not for 0 * NA == NA, you might skip evaluation of y 
> if x evaluates to zero.
> 
> 
> With the same idea, NA * (alpha <- 6) could skip the assignment.

No, it can't, because until it evaluates the assignment it doesn't know 
if it has a class that does strange things with *:

 > `*.foo` <- function(x, y) 42
 > NA*(alpha <- structure("bar", class="foo"))
[1] 42

> I do not think that, on the short term, R would do that thing, but who 
> knows in the future!

As I posted yesterday, this is documented behaviour, so one would hope 
for clear notice if it is going to change.

Duncan Murdoch

> 
> 
> As of R 4.1, typeof(`*`) actually returns "builtin" rather than 
> "special", meaning that all its arguments are evaluated.
> 
> 
> -- 
> 
> Sincerely
> 
> Andre GILLIBERT
> 
> 
> ------------------------------------------------------------------------
> *De :* R-devel <r-devel-bounces using r-project.org> de la part de peter 
> dalgaard <pdalgd using gmail.com>
> *Envoyé :* samedi 28 août 2021 09:26:03
> *À :* Duncan Murdoch
> *Cc :* r-devel using r-project.org
> *Objet :* Re: [Rd] order of operations
> ATTENTION: Cet e-mail provient d’une adresse mail extérieure au CHU de 
> Rouen. Ne cliquez pas sur les liens ou n'ouvrez pas les pièces jointes à 
> moins de connaître l'expéditeur et de savoir que le contenu est sûr. En 
> cas de doute, transférer le mail à « DSI, Sécurité » pour analyse. Merci 
> de votre vigilance
> 
> 
> Yes, and were it not for 0 * NA == NA, you might skip evaluation of y if 
> x evaluates to zero.  In Andre Gillibert's example:
> 
> 1 | (alpha<-6)
> 
> there really is no reason to evaluate the assignment since (1 | any) is 
> always TRUE. Notwithstanding method dispatch, that is.
> 
> With general function calls, all bets are off. Even f(x <- 1) might 
> decide not to evaluate its argument.
> 
> - pd
> 
>> On 27 Aug 2021, at 21:14 , Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>>
>> On 27/08/2021 3:06 p.m., Enrico Schumann wrote:
>>> On Fri, 27 Aug 2021, Gabor Grothendieck writes:
>>>> 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
>>> At least the "R Language Definition" [1] says
>>>   "The exponentiation operator ‘^’ and the left
>>>    assignment plus minus operators ‘<- - = <<-’
>>>    group right to left, all other operators group
>>>    left to right.  That is  [...]  1 - 1 - 1 is -1"
>>> which would imply 2.
>>
>> I think this is a different issue.  There's only one operator in question (the "*").  The question is whether x*y evaluates x first or y first (and I believe the answer is that there are no guarantees). I'm fairly sure both are guaranteed to be evaluated,  under the rules for group generics listed in ?groupGeneric, but I'm 
> not certain the guarantee is honoured in all cases.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-devel using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel 
> <https://stat.ethz.ch/mailman/listinfo/r-devel>
> 
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel 
> <https://stat.ethz.ch/mailman/listinfo/r-devel>
>



More information about the R-devel mailing list