3 ms·
Your examples seem neither clear nor concise though. If I had to try to understand what was happening there without any comments or any understanding what "fib
by sillysaurus3 9y ago
Your examples seem neither clear nor concise though.
If I had to try to understand what was happening there without any comments or any understanding what "fib" was, I'd probably have to stare at that and step through it in my head. Compare that to this:
https://imgur.com/a/80qlG https://imgur.com/a/80qlG
Or this:
https://imgur.com/a/yZNQr https://imgur.com/a/yZNQr
Both are extremely easy to understand, and would remain understandable even for a new programmer.
So without clarity or conciseness, what's left? Why write code using higher order functions by default? There's no win.
- kerkeslager 9y agoI'm not sure on what basis you're saying that my final result is less concise--it's fewer characters than your first solution if you give the functions names which are as non-descriptive. :) As for clarity: given the new programmers our industry is producing, maybe your first solution is easier to understand, but I am not sure that your first proposed solution is actually easier to understand if you assume less prior knowledge of programming. The problem is that we're currently educating people within a procedural paradigm rather than a functional one, so the procedural code is built with components the programmer understands. But if we educated people starting with a higher-order functions instead of for-loops (such as the approach taken in SICP), I'd guess that people would understand the higher-order function way better. Your second solution seems to me to be the clearest and most concise solution of all (and, notably, it uses a functional paradigm). But it is exponentially inefficient--that's the entire point of the article. So I'm not sure why this is even being brought up in this context. All that said, my recommendation would be to follow the dominant paradigm of your environment. If your codebase and language support primarily procedural proramming, write `fib()` with a `for` loop. If your codebase and language support primarily functional programming, write `fib()` with higher-order functions. There isn't really a good case for writing this using tail recursion. EDIT: Also can we never ever post code in Imgur images again? Code is text and transforming it into an image loses all of the benefits of text.