[S] Unusual result

Matt Calder (calder@stat.colostate.edu)
Tue, 24 Mar 1998 21:54:59 -0700 (MST)


S-news,
I came across this today and was surprised by the result. Perhaps
someone could explain this in terms of order of operations.

After:
a <- 0
a <- (a <- a + 1) + (a <- a + 1)

The value of a is 4. I expected 3. In a similar vein,

After:
a <- 0
A <- matrix(0, nrow=3, ncol=3)
A[a <- a + 1, a <- a + 1] <- (a <- a + 1)

The value of A is:

[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 3

I expected that either A[1,2] == 3 or A[2,3] == 1, but not A[3,3] == 3. It
would appear that all of the assignments are evaluated, then the value of
a is inserted in their place. This seems to be at odds with the value of
an assignment being it's right value. The result also seems at odds with
the recently discussed 'commitment of assignments'. If the value of a does
not change until the entire expression is evaluated I would suspect that
A[1,1] == 1.
Anyway, it's a good coffee break amusement. Any help would be
appreciated.

Matt

/*****************************************/
/* Matt Calder, Dept. of Statistics, CSU */
/* http://www.stat.colostate.edu/~calder */
/*****************************************/

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news