将递归算法转换为迭代算法的设计模式 [英] Design patterns for converting recursive algorithms to iterative ones
本文介绍了将递归算法转换为迭代算法的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何通用的启发式方法、技巧、技巧或通用设计范式可用于将递归算法转换为迭代算法?我知道这是可以做到的,我想知道这样做时是否有值得牢记的做法.
Are there any general heuristics, tips, tricks, or common design paradigms that can be employed to convert a recursive algorithm to an iterative one? I know it can be done, I'm wondering if there are practices worth keeping in mind when doing so.
推荐答案
您通常可以完全保留递归算法的原始结构,但避免堆栈,通过使用尾调用并更改为继续传递strong>,正如此博客条目所建议的那样.(我真的应该编写一个更好的独立示例.)
You can often entirely preserve the original structure of a recursive algorithm, but avoid the stack, by employing tail calls and changing to continuation-passing, as suggested by this blog entry. (I should really cook up a better standalone example.)
这篇关于将递归算法转换为迭代算法的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文