2 ms·
The word "while" implies thinking about a range of time from beginning to end. In contrast, the non-"while" version lets you stay in the moment. I disagree. T
by dkarl 16y ago
The word "while" implies thinking about a range of time from beginning to end. In contrast, the non-"while" version lets you stay in the moment.
I disagree. The word "while" does connote time, but it's just the word chosen by early programmers to describe a process of performing an action immediately depending on current conditions, with no knowledge of history. Recursion amounts to the same thing -- after you make the mental leap of a tail-recursive optimization. When you climb the next step, you're climbing it because it's part of the definition of climbing the rest of the steps, which was part of the definition of climbing the rest of the steps when you were on the previous step. As I pointed out in another comment, when you're climbing the last step of a twelve-step staircase, you're actually performing twelve "climb up the steps" operations that are nested like Russian dolls. Thinking recursively is only simple when you learn to mentally simplify all of that, basically to think in terms of the "same machine code" you mention in your comment. I don't think we arrive at that "machine code" starting from a recursive definition of climbing the stairs. It seems much more likely that we figure out the implementation (the while loop) long before we figure out (if we ever do) that we can derive that implementation from a recursive definition of the task.
- infinite8s 16y agoThey are only nested given the common assumption (among programmers, at least) that recursion a stack-like model. From the young child's perspective this is probably tail-recursive operation: def up(): if not at top stair: up() Think of recursion in terms of induction. No nested state required.