Re: [R] returning functions inside lapply

2012-04-24 Thread Duncan Murdoch
On 12-04-24 7:34 PM, Ali Tofigh wrote: Many thanks to both Duncan and Bert for clarifying this. Having looked carefully at what you both wrote, my conclusion is the following: in this code below f<- function(x) {function() {x}} b<- lapply(1:3, f) lapply does not actually call f with the values

Re: [R] returning functions inside lapply

2012-04-24 Thread Ali Tofigh
Many thanks to both Duncan and Bert for clarifying this. Having looked carefully at what you both wrote, my conclusion is the following: in this code below f <- function(x) {function() {x}} b <- lapply(1:3, f) lapply does not actually call f with the values 1, 2, and 3. Instead, it calls f three

Re: [R] returning functions inside lapply

2012-04-24 Thread Duncan Murdoch
On 12-04-24 5:13 PM, Ali Tofigh wrote: On Tue, Apr 24, 2012 at 16:57, Duncan Murdoch wrote: I thought that lapply calls f three times and returns a list with whatever f returned. Is this not so? That is so. In each case, f creates a function that looks in its enclosing environment for the va

Re: [R] returning functions inside lapply

2012-04-24 Thread Ali Tofigh
On Tue, Apr 24, 2012 at 16:57, Duncan Murdoch wrote: >> I thought that >> lapply calls f three times and returns a list with whatever f >> returned. Is this not so? > > That is so.  In each case, f creates a function that looks in its enclosing > environment for the variable x to be returned. > >

Re: [R] returning functions inside lapply

2012-04-24 Thread Duncan Murdoch
On 12-04-24 4:22 PM, Ali Tofigh wrote: This has been asked before, but I just cannot figure out why lapply should behave this way given that R uses lazy evalution. Even after reading (or at least trying to read) parts of the R language definition. f<- function(x) {function() {x}} a<- list(f(1),

[R] returning functions inside lapply

2012-04-24 Thread Ali Tofigh
This has been asked before, but I just cannot figure out why lapply should behave this way given that R uses lazy evalution. Even after reading (or at least trying to read) parts of the R language definition. > f <- function(x) {function() {x}} > a <- list(f(1), f(2), f(3)) > a[[1]]() # as expecte