Message-ID: <2468bbc8-713f-4b52-a9cc-ea6f16a52656@montana.edu>
Date: 2026-01-29T22:24:48Z
From: Poole, Geoffrey
Subject: Incorrect vector recycling in rare circumstance
In-Reply-To: <CAD4oTHH1aW-c6RjngfkD+b0AKAzKvf6Xxy0D=CyRJXfb+Cx4SQ@mail.gmail.com>
Thanks, all, for your responses. I think Serguei summarize my experience well with this:
For me the problem is that actual behavior is easy to program but difficult to use in practice.
That said, I certainly do understand Gabriel's point:
On the other side of the coin, if you're arguing that (a+b)/c should act like mapply(f, a, b, c), remember that
(a+b)/c
is really
`/`(`+`(a, b), c)
Which gets to Duncan's point:
it's desirable that the result in x of
x <- (a + b) / c
should be the same as the result of
temp <- a + b
x <- temp / c
and the pragmatic observation by Gabriel:
And here the `+` operator would need information about c, which is not one of its arguments, in order to do the recycling that way. Also, remember that due to lazy evaluation, a, b, and c aren't even evaluated until their values are needed. In your example, c is not needed until the `/` call (specifically its second argument), which is after a + b is completely done. But in the proposed new mapply like order, c would need to be evaluated before a + b in order to get it's length to know how to recycle a and b, which it does not need to do now and which violates lazy evaluation. Combine that with the fact that a, b, and c, could be replaced with function calls and functions are not supposed to have side effects but are allowed to do so and I think we're looking at a can of worms that I personally would not want to try to open.
I seems clear to me now why R is working the way it is. The unfortunate (?) side effect is that the behavior indicated by the term "recycling of vectors" is undefined until it is interpreted within the context of an expressions. Under some circumstance (specifically, three or more vectors, none of which have lengths that are even multiples of one another), one cannot know how the vectors will be recycled in the absence of investigating the structure of the expression. I had believed that the mapply-like recycling behavior was consistent across any expression.
The fact that (a + b) / c can be written as (a+b) |> {`/`}(c) and is interpreted by R as `/`(`+`(a, b), c) makes is clear why recycling behavior is, in fact, dependent upon the structure of an expression.
I appreciate the discussion.
-Geoff
[[alternative HTML version deleted]]