Re: [R] functional (?) programming in r

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Mon, 17 Nov 2008 19:55:40 -0800 (PST)

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 18 Nov 2008 - 04:01:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Nov 2008 - 05:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive