|
|
|
|
|
by bvrmn
2472 days ago
|
|
You chose a very bad example for "imperative code". It seems artificially complicated. def fib(n: int) -> int:
prev, cur = 0, 1
for _ in range(n-1):
prev, cur = cur, prev + cur
return cur
I think most humans will find this variant more readable and understandable then functional one. |
|
Personally, I still find the functional version a bit more direct. The iterative code still relies on me understanding what the program is "doing"; it requires me to hold state in my head and think through "okay, now `prev` has this value and `cur` has this other value" to reason about what's going on.
I would be interested to find some people with minimal programming experience and show them imperative vs functional implementations of simple functions or algorithms and see what they prefer. Maybe I'll try to do a small study on that or something.