[Rd] order of operations

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sat Aug 28 09:26:03 CEST 2021


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

-- 
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



More information about the R-devel mailing list