3 ms·
What's totally missing from this comparison is that languages allow multiple styles. E.g. the Python variant is for some reason not recursive, and you could wri
by detaro 9y ago
What's totally missing from this comparison is that languages allow multiple styles. E.g. the Python variant is for some reason not recursive, and you could write a Python version that looks a lot more like the Go or Lua versions than this Python version. (Which also can be seen as a weakness of Python from a readability point of view: freedom to choose a style also means you can pick the wrong one or a reader can be surprised by more uncommon one)
- shakna 9y agoFor 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.