TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Tail Recursion

29 点作者 bendiksolheim超过 5 年前

5 条评论

lispm超过 5 年前
&gt; Tail recursion is a special way of writing recursive functions such that a compiler can optimize the recursion away and implement the algorithm as a loop instead.<p>Typically I think more like this: a compiler will compile the tail call into a jump-like construct. Tail recursion is then just only one application of this. The compiler won&#x27;t need to recognize that it is a case of recursion or a loop. Tail call optimization (TCO) is the more general form.<p>See Scheme from 1976:<p>LAMBDA, The Ultimate Imperative, by Guy Lewis Steele Jr. and Gerald Jay Sussman, MIT AI Memo 353<p><a href="https:&#x2F;&#x2F;dspace.mit.edu&#x2F;bitstream&#x2F;handle&#x2F;1721.1&#x2F;5790&#x2F;AIM-353.pdf" rel="nofollow">https:&#x2F;&#x2F;dspace.mit.edu&#x2F;bitstream&#x2F;handle&#x2F;1721.1&#x2F;5790&#x2F;AIM-353....</a>
bjoli超过 5 年前
Recursion is the fundamental looping facility. Every kind of loop can be translated to recursion. That&#x27;s why I think all languages that should be compiled to has it.<p>Translating one looping facility to another is usually very painful. Translating a looping facility to recursion is less painful :)<p>Edit: as an exanple: try compiling the common lisp loop macro to python for loops. I am not even sure it is possible. Compiling it to recursive functions (of which python lacks the tail recursion optimized kind) is however a pretty straightforward, but still very much nontrivial, problem.
评论 #21770536 未加载
BlackLotus89超过 5 年前
<a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=-PX0BV9hGZY" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=-PX0BV9hGZY</a> for anyone who doesn&#x27;t know it already
simendsjo超过 5 年前
A nice and clear explanation of why tail recursion is necessary and how it works. I usually don&#x27;t like mathy examples, but it worked fine here. I especially liked the section on trampolines as I didn&#x27;t know how transpilers usually did that. Well worth the read.
oefrha超过 5 年前
This Stack Overflow answer[1] presents an interesting way to do tail call optimization in Python using the Y combinator.<p>[1] <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;18506625" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;18506625</a>
评论 #21770690 未加载