[R] scope of a function + lazy evaluation

2010-05-10 Thread sayan dasgupta
Hey guys, I have a doubt here , It is something simple I guess, what am I missing out here ?? f - function(y) function() y tmp - vector(list, 5) for (i in 1:5) tmp[[i]] - f(i) tmp[[1]]() # returns 5; z - f(6) tmp[[1]]() # still returns 5; it should return 6 ideally right ??? Even if I dont

Re: [R] scope of a function + lazy evaluation

2010-05-10 Thread Patrick Burns
You are missing 'force'. See 'The R Inferno' page 90. In this case you can define: f - function(y) { force(y); function() y} On 10/05/2010 11:06, sayan dasgupta wrote: Hey guys, I have a doubt here , It is something simple I guess, what am I missing out here ?? f- function(y) function()

Re: [R] scope of a function + lazy evaluation

2010-05-10 Thread sayan dasgupta
Hey thanks for your help , But thats not exactly the problem I have See I am fine with tmp[[1]]() being = 5 and not 1; but then for (i in 1:5) tmp[[i]] - f(i) z - f(6) tmp[[1]]() ## should give 6 right ? Because f(6) was last evaluate so in parent.frame() y should be 6 ??? On Mon, May 10,

Re: [R] scope of a function + lazy evaluation

2010-05-10 Thread Duncan Murdoch
sayan dasgupta wrote: Hey guys, I have a doubt here , It is something simple I guess, what am I missing out here ?? f - function(y) function() y tmp - vector(list, 5) for (i in 1:5) tmp[[i]] - f(i) tmp[[1]]() # returns 5; z - f(6) tmp[[1]]() # still returns 5; it should return 6 ideally

Re: [R] scope of a function + lazy evaluation

2010-05-10 Thread Gabor Grothendieck
When you call a function R passes a promise to it for each argument. A promise consists of the unevaluated variable together with the environment in which it should evaluate the variable when time comes to evaluate it. Thus tmp[[1]] contains function(y) y and in the environment of function(y) y