Scala 中的非尾递归递归有多有用? [英] How usable is non tail recursive recursion in Scala?

查看:54
本文介绍了Scala 中的非尾递归递归有多有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于非尾递归递归调用像 Java 一样使用堆栈帧,所以我会厌倦进行任何递归,比如说超过 1,000 次.因此,我会厌倦将它用于大多数事情.

Since non tail recursive recursion calls use stack frames like Java does, I'd be weary to do any recursion that, let's say goes beyond 1,000 times. I would be thus weary to use it for most of things.

人们真的在 Scala 中使用非尾递归吗?如果是这样,我可以用什么标准来确定它是否可以是非尾递归的?

Do people actually use non tail recursive recursion in Scala? If so, what are the criteria I can use to determine if it can be non tail recursive?

此外,是否有计划在 Scala 的未来版本中取消此内存限制?

Also, are there plans to remove this memory restriction in future versions of Scala?

推荐答案

在你不能使用单尾递归的情况下,比如因为你需要在两个函数之间进行选择,一种叫做蹦床已被描述.

In the situation where you cannot use single tail recursion, for example because you need to alternative between two functions, a mechanism called trampolining has been described.

可以在 Rúnar Bjarnason 的论文 Stackless Scala 中找到对该主题的更近期和更深入的讨论使用免费的 Monad.

A more recent and thorough discussion of this topic can be found in Rúnar Bjarnason's paper Stackless Scala With Free Monads.

就我个人而言,如果您确实遇到这种情况且深度大于 1K,我会走简单的路线并恢复到命令式风格,例如使用可变集合构建器.

Personally, I would go the easy route and revert to imperative style if you really have this situation with >1K depths, e.g. use a mutable collection builder.

这篇关于Scala 中的非尾递归递归有多有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆