<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jun 5, 2012, at 7:37 AM, Ronald Reynolds wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; position: static; z-index: auto; "><div>I hope I'm not too much of a 'pain in the neck noobie' but what is the short clean answer about what's going on when we</div><div>name a function as part of the definition of itself.. This seems pretty esoteric to me. What does the system do? </div></div></div></blockquote></div><br><div>By "what's going on", are you talking about how it's implemented inside the computer, or how people use this technique to solve problems?</div><div><br></div><div>For the former, you would need to know something about memory organization, machine or assembly language, stacks, etc.</div><div><br></div><div>For the latter, let's try this analogy. I'm a custodian, assigned to clean each of the rooms on a long hallway. But I'm lazy, so after cleaning one room I tell my assistant to clean the rest. My assistant is lazy too, so after cleaning one room he tells HIS assistant to clean the rest. That second assistant is equally lazy, so after cleaning one room she tells HER assistant to clean the rest. And so on down the hallway. Eventually my 27th sub-assistant is assigned to clean "the rest of the rooms", but realizes that he's already at the end of the hallway, so he can report that he's finished without cleaning anything at all. His boss reports having accomplished the task she was assigned, as does her boss, and his boss, and his boss, and so on, until word gets back to me that my assistant has accomplished what I told him to do, at which point I announce that I, too, have accomplished what I was told to do.</div><div><br></div><div>Now suppose all of these custodians are not just equally lazy, but are actually clones of one another, or are a bunch of identical robots, or something like that. Every one of them follows the exact same rule: "If there are rooms left to clean, clean one of them and tell my assistant to do the rest. If not, declare success and go home." Since they all follow the same rule, it only needs to be written once.</div><div>Another name for "rule" is "program" or "function"; you can think of the computer as creating a whole bunch of assistants, each one giving the next one an assignment and waiting for him/her to finish.</div><div><br></div><div>Now, what would happen if the custodians were even lazier? When I'm assigned to clean all the rooms on this hallway, I IMMEDIATELY tell my assistant to do the job (the WHOLE job, not "the rest"). My assistant, being equally lazy, immediately tells his assistant to do the job, and so on: soon an infinite number of assistants are telling one another what to do, with nobody ever actually picking up a broom. This is the most common mistake people make in writing recursive programs: they call the same function to solve THE SAME PROBLEM rather than to solve A SMALLER PROBLEM.</div><div><br></div><div>It could be even worse: suppose, after being assigned to clean all the rooms on the hallway, I pick a clean room and hold a drunken party in it, then assign my assistant to clean this room as well as all the rooms I was assigned. My assistant does the same, and rather than having fewer and fewer rooms left to clean, we have more and more. In other words, the function calls itself to solve A LARGER PROBLEM than it itself was given.</div><br><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Stephen Bloch</div><div><a href="mailto:sbloch@adelphi.edu">sbloch@adelphi.edu</a></div></div></span></span>
</div>
<br></body></html>