Javascript 尾部递归优化:为什么'+';不允许吗?

Javascript 尾部递归优化:为什么'+';不允许吗?,javascript,recursion,tail-recursion,Javascript,Recursion,Tail Recursion,我看到的尾部递归的一个常见例子是: function factorial(x) { if (x <= 0) { return 1; } else { return x * factorial(x-1); // (A) } } 我知道这行不通。我想这只是因为它不是真正的尾部递归调用?但这仍然是尾部优化吗?您的最后一个示例不是尾部调用,因为您必须在调用乘法之前继续调用阶乘。考虑: factorial(5) can't call mu

我看到的尾部递归的一个常见例子是:

function factorial(x) {
    if (x <= 0) {
        return 1;
    } else {
        return x * factorial(x-1); // (A)
    }
}

我知道这行不通。我想这只是因为它不是真正的尾部递归调用?但这仍然是尾部优化吗?

您的最后一个示例不是尾部调用,因为您必须在调用
乘法之前继续调用
阶乘。考虑:

factorial(5) can't call multiply until it has the result from factorial(4) can't call multiply until it has the result from factorial(3) can't call multiply until it has the result from factorial(2) can't call multiply until it has the result from factorial(1) can't call multiply until it has the result from factorial(0) 阶乘(5) 在得到阶乘(4)的结果之前不能调用乘法 在得到阶乘(3)的结果之前不能调用乘法 在得到阶乘(2)的结果之前不能调用乘法 在得到阶乘(1)的结果之前无法调用乘法 在得到阶乘(0)的结果之前,无法调用乘法 只有在这一点上,您才停止递归并调用
multiply
。所以,没有尾声

可能还值得注意的是,TCO几乎只由Safari的JavaScriptCore实现。它不是由Chrome的V8或Firefox的SpiderMonkey实现的,也不可能是,规范或无规范:-)更多

我应该注意到,在你的标题中,你问

为什么“+”不允许这样做


是的。TCO并不关心操作是什么-
*
+
相比-只关心它处于尾部位置。

在您的示例中,什么是
乘法
?TCO仍然几乎在所有地方都不受支持。@Grant Everett:请更改您的标题,我很乐意“投票”您的问题。请“向上投票”并“接受”T.J.Crowder最优秀的回答,如果它回答了你的问题。我对这里的提问还不熟悉。我是不是把它改成“已解决”?非常感谢!这真的很有帮助:)
function factorial(x) {
    if (x <= 0) {
        return 1;
    } else {
        return multiply(x, factorial(x-1)); // (A)
    }
}
factorial(5) can't call multiply until it has the result from factorial(4) can't call multiply until it has the result from factorial(3) can't call multiply until it has the result from factorial(2) can't call multiply until it has the result from factorial(1) can't call multiply until it has the result from factorial(0)