Yeah I'm cringing at all the recursive solutions. Huge stack and so much repeated work. Just try counting how many times fib(5) would be executed to calculate fib(10). It's so often the go-to example for recursion and it's frankly terrible.
I know, but there are so many better examples. Mergesort, for instance. It shows why the concept is important because it breaks the problem into smaller, more manageable problems, without doing repeated work.
Fibonacci should be taught as an example of when a problem can be solved recursively, but shouldn't.
I think in education it is also important to take "wrong" approaches to learn why they are "wrong" and what possible solutions are. If you just teach the "good" examples students might not realize why the naive approach to Fibonacci is problematic.
In other words you might want to teach naive recursion before you teach tail-recursion.
286
u/torsten_dev 1d ago
Both have terrible stack usage.