scope of a function + lazy evaluation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

scope of a function + lazy evaluation

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 evaluate the function tmp[[1]] before i.e I do
rm(list=ls())
f <- function(y) function() y
tmp <- vector("list", 5)
for (i in 1:5) tmp[[i]] <- f(i)
 z <- f(6)
tmp[[1]]() # it still returns 5; it should return 6 "ideally" right ???

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: scope of a function + lazy evaluation

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() 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 evaluate the function tmp[[1]] before i.e I do
> rm(list=ls())
> f<- function(y) function() y
> tmp<- vector("list", 5)
> for (i in 1:5) tmp[[i]]<- f(i)
>   z<- f(6)
> tmp[[1]]() # it still returns 5; it should return 6 "ideally" right ???
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

--
Patrick Burns
[hidden email]
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: scope of a function + lazy evaluation

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, 2010 at 3:44 PM, Patrick Burns <[hidden email]>wrote:

> 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() 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 evaluate the function tmp[[1]] before i.e I do
>> rm(list=ls())
>> f<- function(y) function() y
>> tmp<- vector("list", 5)
>> for (i in 1:5) tmp[[i]]<- f(i)
>>  z<- f(6)
>> tmp[[1]]() # it still returns 5; it should return 6 "ideally" right ???
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>
> --
> Patrick Burns
> [hidden email]
> http://www.burns-stat.com
> (home of 'Some hints for the R beginner'
> and 'The R Inferno')
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: scope of a function + lazy evaluation

Duncan Murdoch-2
In reply to this post by sayan dasgupta
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" right ???
>  

No, each time you call f you create a new y variable in its local
evaluation frame.  So all 6 of your y variables are different.  However,
the first 5 of them are all defined by the same expression, i.e. "i".  
Thus the first time they are evaluated they will each get the current
value of that variable.  After the first evaluation, the value will be
fixed, because that is when the value of y is forced.  So for example,

 > f <- function(y) function() y
 > tmp <- vector("list", 5)
 > for (i in 1:5) tmp[[i]] <- f(i)
 > tmp[[1]]() # returns 5;
[1] 5
 >
 > i <- 10
 > tmp[[2]]()
[1] 10
 > tmp[[1]]()
[1] 5

> Even if  I dont evaluate the function tmp[[1]] before i.e I do
> rm(list=ls())
> f <- function(y) function() y
> tmp <- vector("list", 5)
> for (i in 1:5) tmp[[i]] <- f(i)
>  z <- f(6)
> tmp[[1]]() # it still returns 5; it should return 6 "ideally" right ???
>  

See above.

Duncan Murdoch
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: scope of a function + lazy evaluation

Gabor Grothendieck
In reply to this post by sayan dasgupta
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 there is a promise y to evaluate i the
first time that y is actually used.  When you write tmp[[1]]() the
promise is evaluated and since i is 5 at the point that is what you
get.  If f had actually used y when it was called then you would have
gotten 1.

On Mon, May 10, 2010 at 6:06 AM, sayan dasgupta <[hidden email]> 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" right ???
>
> Even if  I dont evaluate the function tmp[[1]] before i.e I do
> rm(list=ls())
> f <- function(y) function() y
> tmp <- vector("list", 5)
> for (i in 1:5) tmp[[i]] <- f(i)
>  z <- f(6)
> tmp[[1]]() # it still returns 5; it should return 6 "ideally" right ???
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.