本文介绍了R 统计环境上的尾递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R 是否支持正确的尾递归?在哪里可以找到相关文档?
Does R support proper tail recursion and where can I find documentation about this?
推荐答案
很容易发现R不支持尾递归优化:
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 统计环境上的尾递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!