[Rd] Strict seq: a recommendation
Dominick Samperi
dsamperi at DecisionSynergy.com
Wed Aug 23 17:13:06 CEST 2006
In for loops of the form:
for(m in seq(beg,end))
...
when beg > end, the default behavior is
to decrement, and the action described
by ... is executed at least once.
On the other hand, if you view this
construction as a translation of the C code:
for(m = beg; m < end; m++)
...
then the semantics of C/C++ is not
respected, because the code in ... is
not executed when beg > end in
the case of C/C++.
To get the proper C/C++ semantics I
use the following replacement for seq:
seqStrict <- function(beg, end, step=1) {
if((beg >= end && step > 0) || (beg <= end && step < 0))
val <- c() # null vector
else
val <- seq(beg,end,step)
val
}
Perhaps it would be useful to add an option to
seq, so seq(beg,end,strict=TRUE) amounts to
the same thing as using seqStrict(beg,end).
ds
More information about the R-devel
mailing list