Re:Computer History Museum 2018 Fellow: Guido van Rossum
by analog31 on Thursday February 22, 2018 @08:16AM
(#16437311)
To be fair, here's plain English (from Wikipedia, re-entered by me):
The sequence f(n) of Fibonacci numbers is defined by the recurrence relation:
f(n) = f(n-1) + f(n-2),
with seed values
f(0) = 0 and f(1) = 1
And a possible variant in Python:
f = [0, 1]
for i in range(100):
f.append(f[-1] + f[-2])
Re:Computer History Museum 2018 Fellow: Guido van Rossum
by joshuamorton on Thursday February 22, 2018 @12:15PM
(#16439193)
heck, if you really want that:
@functools.lru_cache
def fibs(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibs(n-1) + fibs(n-2)
Technically you could also write it as def f(n):
if n in {0, 1}:
return n
return f(n-1) + f(n-2)
which is a bit trickier, but arguably better matches the way the seed values are described in english.
Re:Computer History Museum 2018 Fellow: Guido van Rossum
by detaro on Thursday February 22, 2018 @08:04AM
(#16437245)
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)
Re:Computer History Museum 2018 Fellow: Guido van Rossum
by shakna on Thursday February 22, 2018 @08:10AM
(#16437272)
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.
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.
In the end, it's subjective. And "understandable" and "plain English" may not sit together well, as it can lead to overly verbose code.
But, here's a few examples for you to judge between. (Examples lifted from RosettaCode, which is well worth the look.)
All these languages have claimed, at some time or another, to be easy to read, and close to "plain English". Obviously, this is just a taste.
Python
Ada (A safety-oriented language) AppleScript (Seeing less support from Apple these days, but part of macOS) BASIC (Easy to read, easy to use, easy to get lost.) Clojure (A modern Lisp, bits of Scheme, Common Lisp and Java) COBOL, it's so ridiculously verbose, I'm not adding it here. See https://www.rosettacode.org/wiki/Fibonacci_sequence#COBOLCoffeeScript (Billed as a better JavaScript, it was popular until ES2016)
Delphi (One of my first programming languages. Probably the least verbose staticly typed language without inference I've seen.) Elixir (Kinda like Ruby, but running atop Erlang's massively-concurrent VM.) Elm (A new language for the web. More a framework that replaces HTML, CSS and JS than just JS.) FORTRAN (I've done too much professional work with FORTRAN to like it. It looks straight-forward, but you can do pretty much whatever you feel like with memory, and it'll let you.) Go (Google's somewhat naive language, can be odd to set up, but is fast, static and fairly readable. Lots of people switch from Python to Go.) Julia (It's a language aimed at scientific application. That means a great number system, a really good compiler, and access to things you usually need FORTRAN for.) Lua (Fast but small. Standard library isn't much bigger than C, but has LuaRocks, a decent package manager, with an active community. Some think of it as a better JavaScript, though it was designed for embedding and has a fantastic C-interop story.) Nim (Think Python, but staticly typed, and much faster.)