[Rd] unexpected behavior in list of lexical closures (PR#14004)
elliott.forney at gmail.com
elliott.forney at gmail.com
Tue Oct 13 07:50:09 CEST 2009
Full_Name: Elliott Forney
Version: 2.9.2
OS: Linux, Fedora 10
Submission from: (NULL) (129.82.47.235)
The following code creates a list of functions that are lexically closed over a
single argument. If a print statement is included then each function in the
list evaluates to a different value. If the print statement is not included
then each function evaluates to something different, as expected. I am
convinced that this behavior is not correct as one would not expect the presence
of a lone print statement to alter the behavior of a program.
Please let me know if you have any questions and thank you for your time. The
sample code follows:
## returns a function that sums input with 5
funk <- function(input)
{
## expected behavior if either of
## the following lines is uncommented
# print(input)
# input <- input
## function to sum input with 5
## lexical closure over input
function()
{
input + 5
}
}
## create a list different funk's
funk.list <- list()
for (i in 1:5)
{
## just some values to sum over
test.vector <- 1:i
## add funk that sums test.vector with 5
funk.list[[i]] <- funk(sum(test.vector))
}
## print result of evaluating each funk
## They are all the same unless print or
## reflexive assignment is uncommented!!
for (i in 1:5)
print(funk.list[[i]]())
More information about the R-devel
mailing list