RAY CATZEL wrote: > > Hi Frank, > Thanks for your explanation. I'm getting there - but not quite. I will list the procedure, then the output, then my question: > > to main > carefully [remove "text1 ] [] newtext "text1 [-300 200] [400 400] > top ct > question [Give me a word ==> ] > sculpture answer > end > > to sculpture :wrd > if 1 = count :wrd[pr :wrd stop] > pr :wrd > sculpture bl :wrd > pr :wrd > end > > Now the result: > > caggiano > caggian > caggia > caggi > cagg > cag > ca > c > ca > cag > cagg > caggi > caggia > caggian > caggiano > > Now my question: I understand how the word gradually gets truncated by one letter at a time. I do not > understand how it "grows" again. Which instructions are causing it to add one letter at a time after > "caggiano" >is reduced to "c". > Ahh, where's a white board when you need one :-) A good question and with your question we see the root of the problem. The word "caggiano" passed to the first call of sculpture never shrinks or grows. Sculpture is called repeatedly. Each time the line "sculpture bl :wrd" is executed a new instance of the sculpture routine is started. The argument it receives IS truncated (via the BL) but this operation in no way affects the contents of :wrd in the calling procedure. Are you familiar with the gizmo used in cafeterias to dispense trays? As you take one off the top the one under pops up to take its place. And when you put trays back in they push down the trays already there. This is a stack. Things added to the stack "push" down the things already on the stack so that the first thing in is at the bottom. As you remove things you "pop" them off the top of the stack until you reach that first item. (Push and pop are the two basic actions performed on a stack. Push adds the thing to the top of the stack; pop removes the thing at the top of the stack). So why all this talk of cafeteria trays and stacks. Recursion behaves in the same way. Think of sculpture as a tray. When main calls sculpture a "tray" is pushed onto the stack and its instructions begin executing. Lets say we call it with "cat" as the argument. (Like I said I find it easier to trace these things through with small arguments, makes tracking it easier). The conditional doesn't fire. We print "cat". We call sculpture with "ca". Ahh, we need a new sculpture tray. We get one, give it "ca" as it argument and push it on the stack. Again the conditional doesn't fire. We print "ca". We call sculpture "c". Ahh, again we need a new sculpture tray. It's pushed onto the stack with "c" as the argument. Ok, were are we? We have three sculpture trays on the stack. We just called the last one (The one at the top of the stack) with "c". Now the conditional fires. We print the "c" and STOP. The stop causes this procedure to immediately return so we pop it off the stack and give it the heave-ho. Now what's on the top of the stack? The sculpture "tray" with the argument "ca". When we return to this "tray" we just continue executing at the next instruction after the call. What is it? It's the third print. So we print "ca" (again). We then fall off the end of the procedure. We pop this one off the stack. Again we have a sculpture "tray" waiting. So we resume executing its instructions. We print "cat" (again with the print AFTER the call to sculpture). We fall of the end of this one, pop it and loose it. Now where are we? (At the end of the line, "I'll have the Jell-O and mashed potatoes, thanks). Now we have a main "tray" (It was pushed on the stack when we began execution). We continue execution after the call to sculpture and when we fall off the end of main we're back at the top level. Now you can also see why if we replace the STOP with the STOPME we only get the word decreasing. Once the STOPME in the third instance of sculpture is executed no other instructions in this program execute. It will help a lot if you take a pencil and paper and write down the calls and arguments as you work through this. Recursion is wonderful, great, powerful, elegant and a real show stopper. Plus you can dazzle your friends and co-workers. (I keep hearing the genie from Aladdin speaking these lines). But it ain't easy (at least at first). It's a very different way to think about problems and their solutions. You may want to check out one of the many books mentioned here for a more detailed explanation and for some examples and problems to work out. I hope this helps and didn't make the waters muddier, regards. -- Frank Caggiano caggiano@atlantic.net http://www.atlantic.net/~caggiano --------------------------------------------------------------- Please post messages to the Logo forum to logo-l@gsn.org. Mail questions about the list administration to logofdn@gsn.org. To unsubscribe send unsubscribe logo-l to majordomo@gsn.org.
Global SchoolNet Foundation -
Linking Kids Around the World!
Copyright GSN - All Rights Reserved
- Comments
& Questions
Visit GSN's
Global
Schoolhouse for more exciting learning resources!
Search our Site
-
Home