为什么@tailrec不允许这个功能?
continue
I want to do ln(N!) in Scala with tailrec
@tailrec
final def recursiveLogN(n: Int): Double = {
if (n <= 1) {
return 0
}
Math.log(n) + recursiveLogN(n - 1)
}
The compile error:
could not optimize @tailrec annotated method recursiveLogN: it contains a recursive call not in tail position
Math.log(n) + recursiveLogN(n - 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
递归调用应该是最后一个方法调用,您的摘要的写作方式只是最后一个表达式的一部分。
它可以改写为类似的东西:
然后可以消除尾声
The recursive call should be the last method call, the way your snippet is written it is just part of the last expression.
It can be rewritten to something like:
And then tail call elimination will be possible