R统计环境下的尾递归 [英] Tail recursion on R Statistical Environment
本文介绍了R统计环境下的尾递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R是否支持适当的尾递归以及哪里可以找到有关这方面的文档? 解决方案
很容易找到R不支持尾递归优化:
f < - function(n){
if(n!= 0)f(n-1)
}
f(100000)
#错误:评估嵌套过深:无限递归/选项(表达式=)?
如果tail调用被优化为跳转,那么这个函数就会终止而没有问题。
Does R support proper tail recursion and where can I find documentation about this?
解决方案
It's quite easy to find out that R does not support tail recursion optimization:
f <- function(n) {
if (n != 0) f(n-1)
}
f(100000)
# Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Had tail calls been optimized to jumps, then this function would have terminated without problems.
这篇关于R统计环境下的尾递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文