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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Tail Recursion Tactics: Fibonacci

102 点作者 desio大约 7 年前

10 条评论

tom_mellior大约 7 年前
Silly question: Both this post and the earlier one linked from it show that a manually written loop easily beats the tail-recursive version. That is, Go doesn't seem to do tail recursion optimization. In which case, why bother with all of this?
评论 #16557745 未加载
评论 #16557947 未加载
dwilding大约 7 年前
I love your explanation of how to arrive at the vector transformation function. You might enjoy reading an article I wrote a few years ago that touches on maxtrix&#x2F;vector computation of linear recurrences: <a href="http:&#x2F;&#x2F;aperiodical.com&#x2F;2014&#x2F;06&#x2F;discovering-integer-sequences-by-dealing-cards&#x2F;" rel="nofollow">http:&#x2F;&#x2F;aperiodical.com&#x2F;2014&#x2F;06&#x2F;discovering-integer-sequences...</a><p>Not as detailed as your post though. Thanks for a great write up!
评论 #16561601 未加载
cousin_it大约 7 年前
If you&#x27;re computing Fibonacci numbers using only addition, no matter how clever your recursion, you will always lose out to algorithms that use multiplication. <a href="https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;fast-fibonacci-algorithms" rel="nofollow">https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;fast-fibonacci-algorithms</a>
评论 #16560374 未加载
评论 #16561524 未加载
kerkeslager大约 7 年前
Tail call optimization is a clever, but even in functional languages, twisting your code around to use tail calls is often a code smell. Most uses of tail recursion would be better-served by using some higher-order functions. The reason is that when you write something tail recursively, it&#x27;s sort of like rolling your own iteration.<p>This is particularly true because many platforms don&#x27;t supply tail call optimization, but do supply higher-order function.<p>For example, Python 3:<p><pre><code> import functools import itertools def fib(n): def get_next(i): a, b = i return (b, a + b) a, b = functools.reduce( get_next, itertools.repeat(None, n), (0, 1), ) return b </code></pre> This does come out a bit hacky in Python, because Python doesn&#x27;t have a builtin higher-order function that does something like this:<p><pre><code> def generate(get_next, c): while True: yield c c = get_next(c) </code></pre> But many (most?) functional languages have such a function. With that function built in, the code would look something like:<p><pre><code> import itertools def fib(n): def get_next(i): a, b = i return (b, a + b) a, b = next(itertools.islice( generate(get_next, (0, 1)), n, )) return b </code></pre> Further, most functional languages have a builtin function that gets the nth item in the sequence, which is what `next` and `itertools.islice` are doing above:<p><pre><code> def nth(seq, n): return next(itertools.islice(seq, n)) </code></pre> If that were built in also, we get:<p><pre><code> def fib(n): def get_next(i): a, b = i return (b, a + b) a, b = nth(generate(get_next, (0, 1)), n) return b </code></pre> This gives us some pretty terse code built only of composable builtin pieces, and ostensibly these higher-order functions are written in a lower-level language and highly optimized. This is cleaner than rolling your own tail recursion in a lot of ways.
评论 #16560955 未加载
评论 #16559692 未加载
评论 #16559818 未加载
ArmandGrillet大约 7 年前
Interesting article with great formatting, I hope there will be more! Blogs like this and YouTube channels like 3Blue1Brown make maths more tangible.
评论 #16558313 未加载
dustingetz大约 7 年前
<a href="https:&#x2F;&#x2F;wiki.haskell.org&#x2F;The_Fibonacci_sequence" rel="nofollow">https:&#x2F;&#x2F;wiki.haskell.org&#x2F;The_Fibonacci_sequence</a>
评论 #16561545 未加载
评论 #16558980 未加载
enedil大约 7 年前
Unfortunate, that your results for `n = 90` are completely useless, since F_90 &gt; 2^32 which is the size of int.<p>Also, you could have shown a method which is O(log n), namely [[F(n+1) F(n)], [F(n) F(n-1)]] equals [[1, 1] [1, 0]] raised to the power of n.
评论 #16558946 未加载
OJFord大约 7 年前
Great post, please add RSS for at least one subscriber!
评论 #16558278 未加载
taeric大约 7 年前
Would be neat to see how the vector version works using successive squares.
chx大约 7 年前
When I see func FibTailVecSum(n int) int { if n &lt; 2 ... and func FibTailVec(acc int, a int, b int) (int, int) { if acc == 1 { I go full Terry Pratchett and began to yell Guards! Guards! I mean, these are perfect examples of Elixir guards. Is there a similar functionality in Go?