1. First "a" gets the sum of two expressions.
2. The two expressions each are "a" .
3. Do the right one first. a<-a+1, which is 1.
4. Do the left one; a <-a+1, which is now 2. (they must be done
sequentially in a Turing machine).
5. Work back up the tree to evaluate a+a, which is now 4.
So the behavior is entirely according to Hoyle (or to Turing).
-- Bert Gunter
Merck Research Labs
> >>>>> "MW" == Matt Calder <calder@stat.colostate.edu> writes:
>
> MW> S-news, I came across this today and was surprised by the
> MW> result. Perhaps someone could explain this in terms of order of
> MW> operations.
>
> MW> After: a <- 0
> MW> a <- (a <- a + 1) + (a <- a + 1)
>
> MW> The value of a is 4. I expected 3.
>
Berwin Turlach replied:
> Well, I cannot explain it in terms of order of operations. My guess
> is, that this sequence of instruction eventually boils down to the
> execution of the following instructions:
> 1) fetch `a', add 1 to it and store it in `a' [first a<-a+1, now a=1]
> 2) fetch `a', add 1 to it and store it in `a' [second a<-a+1, now a=2]
> 3) to form the addition, we need the result of the code inside the two
> ()'s, but that is stored in `a' [first ()] and `a' [second ()].
> Hence,
> fetch `a' and `a', add these two numbers and store the result in `a'.
> Result: a=4.
>
> It seems to be the S+ equivalent to i=i++ in C. You are probably
> lucky that S+ didn't reformat your disk :)
>
> Best wishes,
>
> Berwin
>
> =============================== Full address
> ================================
>
> Berwin A Turlach
> Centre for Mathematics and its Applications Tel.: +61 (2) 6249 0706
> (Secr.)
> Statistical Science Program +61 (2) 6249 0731
> (direct)
> The Australian National University FAX : +61 (2) 6249 5549
> Canberra ACT 0200 e-mail:
> berwin.turlach@anu.edu.au
> Australia
> http://wwwmaths.anu.edu.au/~berwin
> ----------------------------------------------------------------------
> -
> 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
>
-----------------------------------------------------------------------
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