News for hackers. Stuff that matters.
Re:It Can Happen to You by rectang on Wednesday March 03, 2021 @08:32PM (#26337455)
Pro tip: the classic recursive approach to implementing the Fibonacci sequence exhibits eye-wateringly poor performance.
Re:It Can Happen to You by sjburt on Wednesday March 03, 2021 @09:16PM (#26337783)
I've always hated how that's used as an example of recursive programming in early programming education. Invariably, students try a large n, get a stack overflow and conclude that recursion is inefficient, leads to mysterious errors, and must be avoided.
Re:It Can Happen to You by saagarjha on Thursday March 04, 2021 @06:56AM (#26341303)
Fibonacci is generally more likely to take forever than stack overflow.
Re:It Can Happen to You by elseweather on Wednesday March 03, 2021 @08:49PM (#26337578)
...which makes it a classic exercise for teaching memoization!
Re:It Can Happen to You by astine on Wednesday March 03, 2021 @09:02PM (#26337675)
And now your memory usage will grow eye-wateringly large. Instead, convert the algorithm to be iterative or at least tail-recursive and it will be faster than both the naive and memoized versions!
Re:It Can Happen to You by karamazov on Wednesday March 03, 2021 @09:03PM (#26337688)
Or use the closed-form solution.
Re:It Can Happen to You by jacobolus on Wednesday March 03, 2021 @09:39PM (#26337947)
The "closed-form solution" is slower than standard method. It just uses arbitrary-precision fractional arithmetic (square root, exponentiation, division) instead of arbitrary-precision integer arithmetic (exponentiation of a 2x2 integer matrix).
Re:It Can Happen to You by singhrac on Wednesday March 03, 2021 @09:53PM (#26338042)
The best solution is to use the O(log n) time exponentiation of a matrix, which is fast enough to be constant.
Re:It Can Happen to You by jbluepolarbear on Wednesday March 03, 2021 @08:43PM (#26337531)
Looped algorithms generally outperform recursive algorithms.
Re:It Can Happen to You by Tyr42 on Wednesday March 03, 2021 @09:45PM (#26337992)
Goto based algorithms generally outperform looped algorithms.

(They're just more general, you can always implement a loop based algorithm using gotos, but not the other way around)

Re:It Can Happen to You by ThatGeoGuy on Thursday March 04, 2021 @01:01AM (#26339158)
Not exactly. This is more an artefact of language design.

If you convert everything to continuation passing style, then every function call, tail call, recursion, etc. is just as expensive (and expressive) as a GOTO. This is, incidentally, the main "trick" or lightbulb moment in the classic Cheney on the MTA paper by Henry Baker [1].

Now if we're talking specifically in C, then absolutely! But while this intuition holds for C, it's not always true and doesn't have to be always true.

[1] https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.54...

Re:It Can Happen to You by jbluepolarbear on Thursday March 04, 2021 @02:57AM (#26339860)
That was a really good short read.
Re:It Can Happen to You by didibus on Wednesday March 03, 2021 @09:56PM (#26338062)
Recursion is a form of looping. I think it's more clear if you say imperative algorithms.
Re:It Can Happen to You by arcturus17 on Thursday March 04, 2021 @04:01AM (#26340239)
In what sense is recursion a form of looping?

I'm thinking of the memory model for each - one mutates some variables (at least some sort of index, unless you're doing an infinite loop) in a single frame, while the other accumulates function calls and stack frames until you bottom out, then return values are "folded" back up.

Semantically, from a caller's perspective, they can achieve the exact same thing, but aside from that they seem radically different to me - is there anything else that could lead us to say that recursion is a form of looping?

Re:It Can Happen to You by didibus on Thursday March 04, 2021 @01:07PM (#26345830)
> In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met.

That's the general definition at least I've always been most aware of. I don't want to claim it is the most common one, cause I don't really have numbers and who is the authority on comp-sci definitons? But I do feel it is at least a somewhat common academic definition for looping.

That would mean that recursion is a form of looping. The distinction between recursion and imperative loops would then be a matter of implementation and exposed programmer interface. Similarly like others have said, goto's can also be used to implement similar loops.

And in that regard, there are variants of recursion as well, such as tail-call recursion which has a similar memory model as what you described and differs only to an imperative loop in the programming interface it exposes.

Re:It Can Happen to You by arcturus17 on Thursday March 04, 2021 @01:55PM (#26346544)
There's no denying that from that definition they are the same. It's just after you've debugged enough loops and recursions you can't help but think they are quite different!
Re:It Can Happen to You by didibus on Friday March 05, 2021 @01:49AM (#26353646)
Well, I don't mean they are the same, they're just different kinds of looping constructs.

Like what you call a loop isn't a loop, its actually a for-loop, or its a while-loop, or a for-each loop, or its an iterator loop, and similarly recursion is just a recursive loop.

At least that's the common taxonomy I know off. So all these are loops, and the ones that involve mutation for the condition to kick in are further grouped as imperative loops.

Re:It Can Happen to You by beaconstudios on Wednesday March 03, 2021 @08:46PM (#26337556)
does that hold for languages/compilers with tail call optimisation?
Re:It Can Happen to You by desertrider12 on Thursday March 04, 2021 @12:22AM (#26338943)
This isn't really related to your question, but I don't think tail calls could help for Fibonacci since f(n) branches to two calls, f(n-1) and f(n-2). And each of those branches into 2. So it can't be done in a finite stack area with naive recursion.

The compiler would either have to memoize, or be extremely clever and start at the base case (0, 1) and then transform the code to use the 2x2 matrix exponentiation. I wouldn't have been suprised if GHC haskell was that clever, but even with -O2 "print $ fibb 10000" isn't terminating.

Re:It Can Happen to You by iamevn on Thursday March 04, 2021 @02:10AM (#26339554)
Wouldn't this be the recursion version with tail calls?

  (define/contract (fib n)
    (-> nonnegative-integer? nonnegative-integer?)
    (let fib-recur ([i 1] [curr 1] [prev 0])
      (cond [(= i n) curr]
            [else (fib-recur (+ i 1) 
                             (+ curr prev) 
                             curr)])))
Re:It Can Happen to You by desertrider12 on Thursday March 04, 2021 @09:36PM (#26351819)
That's pretty much what I meant by the matrix exponentiation method - applying the map (a, b) -> (a+b, a) repeatedly. Your function definitely uses tail calls, but I was just trying to say more than tail call optimization is needed to transform the trivial recursive version

fibb 0 = 0

fibb 1 = 1

fibb n = + (fibb (- n 1)) (fibb (- n 2))

into that, or any O(n) version.

Re:It Can Happen to You by kaba0 on Thursday March 04, 2021 @04:43AM (#26340477)
Haskell is not that clever (or at least it was not a year ago). The memoized version is much much faster.
Re:It Can Happen to You by adrianN on Thursday March 04, 2021 @01:18AM (#26339243)
If you replaced the recursion with loops but implemented the same algorithm you'd just have to manage a stack on the heap. I don't think that would be faster.
Re:It Can Happen to You by jbluepolarbear on Wednesday March 03, 2021 @09:15PM (#26337768)
Tail calling is basically looping and why I said generally.
Re:It Can Happen to You by hydroxideOH- on Wednesday March 03, 2021 @09:05PM (#26337699)
At least for Racket, which does have TCO, the for-loop is really a macro for a tail recursive loop. I'm not sure about other languages with more aggressive optimization though.
Re:It Can Happen to You by exporectomy on Wednesday March 03, 2021 @09:23PM (#26337833)
And don't cause stack overflows in normal operation. I've run into that before and now avoid recursion.