Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
0 of .
Results for:
No results containing your search query
P. 1
Chapter 9. Recursion

Chapter 9. Recursion

Ratings: (0)|Views: 54 |Likes:
Published by scribdmuthu

More info:

Published by: scribdmuthu on Jan 27, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Chapter 9. Recursion
“Well, you sure picked a good subject this time,” saidMorf, looking a bit puzzled.“We touched on this before, don’t you remember?”replied Logy. “You remember, recursion is when a procedureuses itself as part of the solution.”“Say what?”Yes, strange as that may seem, arecursive procedure is one that calls itself as part of the total solution. You sawexamples of this in earlier chapters.It is something like the two turtles inthe picture. Each turtle is using the otherto draw itself.________________________________________________
Is Life Recursive
Here’s a fun procedure to help you make some sense outof recursion, even though it really isn’t Logo.TO GET.THROUGH.LIFEGET.THROUGH.TODAYGET.THROUGH.LIFEENDThink about it for a moment. This says that to get throughlife, you have to get through today. Once you are throughtoday, you have to move on, right?But where?
You can't go backward. You can't stop time. You haveto get through life. But to get through life, you have to getthrough today. Each day is different. So while this may look like a simple loop, it really isn't. Have you ever repeated aday, doing exactly what you did yesterday?Let's add another twist to this. Let's suppose that whenyou're standing in front of the Pearly Gates to Heaven, you areasked to take a look at your Book of Life.Embedded recursion can actually help you with this task.We talk about different types of recursion in the next section.Anyway, to see your Book of Life, all you have to do is add acouple of lines to your life procedure.TO GET.THROUGH.LIFEIF LIFE = "OVER [STOP]GET.THROUGH.TODAYGET.THROUGH.LIFEPRINT BOOK.OF.LIFEENDIt's like every time you GET.THROUGH.TODAY, youwrite a page in your Book of Life. Only the pages are notprinted until LIFE = "OVER. When the procedure stops, allthe pages are printed starting with the last page it saved.This may seem very confusing right now. But it will beginto make sense very shortly.
Tail-end Recursion
Yes, recursion is confusing.It reminds you of the images you see when you look intwo mirrors — without Morf getting in the way, that is. Solet’s try it with something we know something about.TO MAZE :NFD :N RT 90MAZE :N + 5ENDThis is an example of what you call “tail-end recursion.”The recursive call is at the tail-end of the procedure.To see just how this works, typeMAZE 20Now watch what happens.The turtle goes forward 20 steps and then turns right 90turns. Then the procedure tells :N (that's 20 to us) to become:N + 5 (that's 25 to us now). Then the procedure saysMAZE 25

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->