[Rd] Strict seq: a recommendation

Dominick Samperi dsamperi at DecisionSynergy.com
Wed Aug 23 18:02:35 CEST 2006


I should point out that this recommendation was not designed just
to accommodate C/C++, it was motivated by problems
with R code of the form:
for(m in seq(beg,end))
val <- x[m]

Take beg=1, end=0, for example. This results in
val <- x[0]

and R issues no error about the subscript being out
of bounds. Later when val is used you get not very
informative error messages about variables of
length zero (because val has length zero after the
above assignment).

ds


Thomas Lumley wrote:
> On Wed, 23 Aug 2006, Dominick Samperi wrote:
>
>> 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++.
>
> There is another important way in which the C loop is different from 
> the R loop.  If you modify m or end inside the loop the number of 
> iterations of the C loop changes but the number of iterations of the R 
> loop doesn't.  R's for() loop just isn't the same as C's.
>
>     -thomas
>
> Thomas Lumley            Assoc. Professor, Biostatistics
> tlumley at u.washington.edu    University of Washington, Seattle
>



More information about the R-devel mailing list