Re:Computer History Museum 2018 Fellow: Guido van Rossum
by dom96 on Thursday February 22, 2018 @08:30AM
(#16437380)
Where 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)
Re:Computer History Museum 2018 Fellow: Guido van Rossum
by detaro on Thursday February 22, 2018 @08:27AM
(#16437363)
Many 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.
Re:Computer History Museum 2018 Fellow: Guido van Rossum
by joshuamorton on Thursday February 22, 2018 @12:16PM
(#16439205)
As 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.
> 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.