63 points · by ColinWright · 9y ago · 58 comments
It had better. If you're allowed to put larger disks on smaller disks, you can solve towers of Hanoi in linear time. Call the rods A, B, and C: the goal is to move the stack from A to C. First flip the stack upside down, one disk at a time, from A to B. Then do that again, from B to C. The stack is now upright on C, after 2n moves.
(In contrast, the real problem requires O(2^n) moves.)
[1] https://www.youtube.com/watch?list=PLZHQObOWTQDMRtm8h9bG9P06...
It seems like without that rule, the game's solution is 2n-1 steps (dump n-1 disks from the left to the right, move disk n to the center, then un-invert those n-1 disks on top of disk n), rather than 2^n-1 moves.
In the case where you can place larger disks on top of smaller disks, the sub-problems don't have the same solutions, i.e. In order to move N disks from A to B you first move N-1 disks from A to C, then one disk from A to B, then the N-1 disks back from C to B. But in order to solve the N-1 disks, you don't need to ensure that the largest disk ends up on the bottom -- you just move them all straight to the target.
I guess the rule is implicitly tied up in the fact that the sub-problems are solved in the same way as the larger problem.
Difference between "variables" in math vs. functional programming? None. The value of a variable can't change.
Difference between "function" in math vs. functional programming? None. It's just a map from one set to another.
Functional programming much better supports equational reasoning.
Imperative programming much better supports operational reasoning.
Or you could contrast "data flow" vs "control flow".
I'd have thought we won't need operational reasoning in 2017 but currently I conclude it may always be a part of computer programming..
f(x) = x^2 + x + 1
the value of x in the right-hand-side expression doesn't change in the same sense that variables in imperative languages change. But it allows the single expression "x^2 + x + 1" to have a value which varies by the value of x. For example, f(2) yields the value 7 while f(3) yields the value 13.Eventually, the guy in the second row asks the guy in the first row, who immediately says he's in the first row. Then each person adds 1 to the answer they get back and feed it back until eventually you get your answer.
Of course, I guess this just highlights how all the toy examples we see in CS courses often use trivial things like computing Fibonacci sequences or factorials, which are much easier to do iteratively. This often leaves students wondering why anyone would ever even use recursion, or at best, makes it seem like recursion vs. iteration is a mere stylistic choice.
Fantastic question. And a point I would respond with is that if you were to actually get up and walk to the front of the cinema counting rows as you go, you would realise that in doing so you would have walked forwards one row and then performed exactly the same action that the person in front of you would have done had they tried to count their row. So even this "iterative" procedure parallels recursion.
Perhaps a problem that seems harder to convert to iteration is that of evaluating arbitrary arithmetic expressions. In order to evaluate a binary expression, you evaluate its two parts (recursively) and then perform the binary operator on the two operands. In order to evaluate a numeric literal, you simply take its value. So (1 + (2 * 3)) involves evaluating 1 to itself, (2 * 3) to 6, and then finally (1 + 6) to 7.
"No, no, no. Look ..."
"But to start you can only move the top-most disk?"
"Yes, but it won't work because ..."
"What? You have to move the top-most as the first move."
(Comment deliberately phrased provocatively to hear push-back and feedback.)
My first encounter with recursion was when I needed to traverse a directory tree. I couldn't figure how to go arbritrarily deep without writing tens of nested for-loops. Then recursion somehow hit me, or I found something interesting on Google, can't remember, it was over 10 years ago, pretty sure SO wasn't around.
...and then a while later I found out about os.walk (this was python).
You just won't get a stack overflow exception with the loop -- you'll run out of heap instead. It's still the stack filling up.
Yes. Maybe there could be some algebra in advance of -- or alongside -- early coding lessons, where this idea is central to both fields. One variable name, any numeric value.
It's the same with functions, the lifeblood of analysis -- one function name, any equation. So in a perfect world, people would be learning calculus and general analysis while writing their first program functions -- indeed, they might be learning higher math while coding their lesson materials at the same time.
I would really like to see a production language, where program-stack based recursion was a compile time error. Instead, only recursion as syntax sugar is allowed. i.e. Only recursion (or any cyclic function call graph) that the compiler can translate to "imperative code", should be allowed to compile.
For two reasons:
1. I honestly dream of a world where we can no longer think about infinitely propagating program-stacks.
2. It would force us to invent better optimization algorithms (like tail-call optimization) but for more general recursive patterns.
- 4 years of self learned programming
- 4 years of university for computer engineering
- 5 years in the industry, as a software engineer
[0] https://en.wikipedia.org/wiki/Continuation-passing_style
Previous discussion: https://news.ycombinator.com/item?id=13793404
https://jugad2.blogspot.in/2013/12/google-gets-recursion.htm...