[R] functional (?) programming in r
Thomas Lumley
tlumley at u.washington.edu
Tue Nov 18 04:55:40 CET 2008
On Mon, 17 Nov 2008, Stavros Macrakis wrote:
>
> Agreed. In fact, the man page for 'for' seems to explicitly specify
> that the iteration variable is not mutable: "The variable var... is
> read-only". However, the implementation doesn't seem to enforce this:
>
> for (i in 1:5) print(i<-i) # no error given for this assignment
>
The assignment is valid, and i is not in fact read-only, it's just that
modifying i does not change the sequence of the loop.
> for(i in 1:5) {i<-10*i; print(i)}
[1] 10
[1] 20
[1] 30
[1] 40
[1] 50
That is, i gets reset to the next element of the sequence at the start of
each iteration of the loop. Modifying i is permitted, there's just no good
reason to do it.
-thomas
More information about the R-help
mailing list