【发布时间】:2013-08-12 16:57:04
【问题描述】:
如何在 R 中转换该伪代码,以使所有绑定都冻结在定义函数时的状态?
lastfunction <- function(inputx) {rep(0,length(inputx))}
i<-1
while(i<=2){
afunction <- function(inputx) {i*inputx} #freeze all variable used in the body
lastfunction <- lastfunction + afunction #and of course the label "afunction" as well
i<-i+1
}
#then apply to some data
lastfunction(c(0,1,5,6))
我查看了环境,但看不到如何正确设置(嵌套环境?)
【问题讨论】:
-
你能用看起来不像 R 的东西重写你的伪代码吗?您正在添加两个函数,但我不明白您的意思。
-
只是在尾注上:看看
?Recall。
标签: r recursion functional-programming