3 ms·
For the most part, I tried to choose styles that match the language, but yes, just a taste of the languages. > E.g. the Python variant is for some reason not r
by shakna 9y ago
For the most part, I tried to choose styles that match the language, but yes, just a taste of the languages.
> E.g. the Python variant is for some reason not recursive
Because of the recursion limit in Python, really, as Python doesn't do tail-call elimination. That limit can make recursive code a real pain point, so iterative styles tend to work better.
- detaro 9y agoMany of these programs do not get tail-call elimination (e.g. I think most languages can not automatically optimize the equivalent of "return f(n-1) + f(n-2)", if they do tail-call elimination at all). The nicest ones do not bother with negative n, some others don't. IMHO not a very useful set of comparisons.
- dom96 9y agoWhere did you find the Nim example? It's by far the strangest fibonacci implementation I have seen (and I am a Nim developer). Anyway, I translated the Go example to Nim for a better comparison: proc fib(a: int): int = if a < 2: return a return fib(a - 1) + fib(a - 2)
- joshuamorton 9y agoAs I mentioned elsewhere, in python the correct way would be to add the @functools.lru_cache decorator. That way you get the caching behavior of the tricky to write methods, with the clarity of the obvious recursive solution.