8 ms·
When you study the old AI programming you eventually realize that non-determinism rules and recursion drools. Recursion is great for certain things that would
by PaulHoule 2y ago
When you study the old AI programming you eventually realize that non-determinism rules and recursion drools. Recursion is great for certain things that would be hard otherwise (drawing fractals) but it shouldn’t be the first tool you reach for. It’s known to be dangerous in embedded systems because it can blow out the stack. Turns very simple O(N) problems into O(N^2) but unfortunately a generation of computer science pedagogues taught people that recursion > iteration because BASIC was the teaching language of the 1970s and first half of the 1980s and they couldn’t get over that Lisp, Forth, UCSD Pascal and other contenders didn’t make it to the finish.
- peterfirefly 2y ago> When you study the old AI programming you eventually realize that non-determinism rules and recursion drools. This part I agree with.
- toolslive 2y agoOk, I'll bite. - Recursion allows you to show correctness both in programming and mathematics. - plenty of compilers will reuse the current stack frame in a tail call.
- samatman 2y agoCounterpoint: https://news.ycombinator.com/item?id=41974171 https://news.ycombinator.com/item?id=41974171
- PaulHoule 2y agoTail call recursion doesn't turn the O(N^2) version of Fibonacci into O(N) but at least memoization turns it to O(N log N) or something. It's true that recursive algorithms can be easy to analyze. If you really knew your math you could do Fibonacci as O(1).
- xyzzyz 2y agoYou cannot do Fibonacci in O(1). You can reasonably do it in O(log n), though. Recursive Fibonacci is not O(n^2) either, it’s exponential.
- deleted 2y ago[deleted]
- tomsmeding 2y agoIf you have O(1) arbitrary-precision floating-point operations in O(1), you can do Fibonacci in O(1) by Binet's formula. But you don't, so you can't. Also, by a similar argument, the O(log n) by matrix exponentiation is really O(n log(n)^2 log(log(n))) by Schönhage-Strassen, I think. (The sequence is exponential in n, so the number of digits is O(n).) I may have miscounted the logs.
- xyzzyz 2y agoYou’re exactly right, I omitted the Schonhage Strassen just to simplify the point.
- WolfeReader 2y agohttps://en.m.wikipedia.org/wiki/Fibonacci_sequence#Closed-form_expression https://en.m.wikipedia.org/wiki/Fibonacci_sequence#Closed-fo... If exponentiation is done using floating-point arithmetic, this is O(1)
- bodhiandphysics 2y agono... you can do fibonacci as O(log n)... you cannot represent (1 + sqrt(5))/2 on a computer.
- Smaug123 2y agoYou literally just did! The problem is not representing the number, it's computing digits.
- xyzzyz 2y agoYou totally can. Here is a O(log n) implementation of the Binet formula with infinite precision: https://github.com/xyzzyz/FibBinet/blob/master/FibBinet.hs https://github.com/xyzzyz/FibBinet/blob/master/FibBinet.hs
- norir 2y agoThere is a simple tail recursive implementation of fibonacci that is O(N) and doesn't use memoization. I'll leave it as an exercise for the reader.
- atn34 2y agoThe natural recursive implementation is exponential, and the memoized version is O(n)