Professional Documents
Culture Documents
Chapter 9. Recursion
“Well, you sure picked a good subject this time,” said
Morf, looking a bit puzzled.
“Say what?”
Is Life Recursive
Here’s a fun procedure to help you make some sense out
of recursion, even though it really isn’t Logo.
TO GET.THROUGH.LIFE
GET.THROUGH.TODAY
GET.THROUGH.LIFE
END
But where?
341
Recursion
TO GET.THROUGH.LIFE
IF LIFE = "OVER [STOP]
GET.THROUGH.TODAY
GET.THROUGH.LIFE
PRINT BOOK.OF.LIFE
END
This may seem very confusing right now. But it will begin
to make sense very shortly.
342
Recursion
Tail-end Recursion
Yes, recursion is confusing.
TO MAZE :N
FD :N RT 90
MAZE :N + 5
END
MAZE 20
MAZE 25
343
Recursion
TO MAZE :N
IF :N = 200 [STOP]
FD :N RT 90
MAZE :N + 5
END
344
Recursion
TO ERECTORSET :N :X
IF :X = 0 [STOP]
SECTION :N
MOVE :N
ERECTORSET :N :X - 1
END
TO SECTION :N
REPEAT 4 [TRI :N FD :N RT 90]
END
TO MOVE :N
RT 90 FD :N LT 90
END
TO TRI :N
FD :N RT 135
FD :N / SQRT 2 RT 90
FD :N / SQRT 2 RT 135
END
345
Recursion
Uh, oh!
________________________________________________
TO CIRCLE :N
REPEAT 36 [FD :N RT 10]
END
TO SPAGHETTI :N
CIRCLE :N + 5
CIRCLE :N + 4
CIRCLE :N + 3
346
Recursion
CIRCLE :N + 2
RT 45
SPAGHETTI :N
END
How about this one?
TO SPAGHETTI :N
CIRCLE :N
IF :N = 0 [STOP]
RT 45
SPAGHETTI :N - 1
END
TO SPAGHETTI :N
IF :N > 200 [STOP]
CIRCLE :N
RT 45
SPAGHETTI :N + 5
END
TO CIRCLE :N
REPEAT 36 [FD :N RT 10]
END
________________________________________________
347
Recursion
TO CIRCLES
CIRCLE 5
FD 20
CIRCLES
END
Embedded Recursion
To check this out, let’s look at a procedure that uses
“embedded recursion.” The recursive call is embedded in the
middle of the procedure somewhere, like in the
GET.THROUGH.LIFE procedure.
TO TEST.RECURSION :N
PRINT [IS THIS RECURSION?]
IF READWORD = "YES [TEST.RECURSION :N + 1]
PRINT :N
END
348
Recursion
TEST.RECURSION 1
When you start the procedure, the first thing you see on
the screen is the question:
IS THIS RECURSION?
IS THIS RECURSION?
PRINT :N
Is this what recursion does to you?
349
Recursion
Well, that isn’t the way recursion works. Morf has one
of his rabbit trails to show you what happens.
________________________________________________
Cut the paper along the folds. You should end up with 16
small pieces of paper. Now number these “pages” from 1 to
16 by writing a small number at the bottom of the page.
TEST.RECURSION 1
350
Recursion
Why?
You should have two stacks of paper now. You have some
blank pages left over in one stack, pages 8 to 16. The other
stack has the pages you numbered from 1 to 7. Each page has
a big number written on it.
Now put the pages back in order from 1 to 16. But how
do you do that?
351
Recursion
TO MAZE :N
IF :N > 300 [STOP]
FD :N RT 90
MAZE :N +10
END
TO AMAZE :N
IF :N > 120 [STOP]
AMAZE :N + 10
FD :N RT 90
END
Try to picture what it will look like before you run it.
Think about how recursion works, about how it reads and acts
on procedures. Then start with :N as 50.
352
Recursion
TO AMAZE 50
IF 50 > 300 [STOP] Since 50 is smaller than 300,
Logo goes to the next line.
AMAZE 50 + 10 AMAZE 50 becomes AMAZE
60 and AMAZE starts again.
FD 50 RT 90 This line is held in memory.
END
TO AMAZE 60
IF 60 > 300 [STOP] Since 60 is smaller than 300,
Logo goes to the next line.
AMAZE 60 + 10 AMAZE 60 becomes AMAZE
70 and is called.
FD 60 RT 90 This line is held in memory.
END
TO AMAZE 70
IF 70 > 300 [STOP] Since 70 is smaller than 300,
Logo goes to the next line.
AMAZE 80 + 10 AMAZE 70 becomes AMAZE
80 and is called.
FD 70 RT 90 This line is held in memory.
END
and then
353
Recursion
TO AMAZE 80
IF 80 > 300 [STOP] Since 80 is smaller than 300,
Logo goes to the next line.
AMAZE 80 + 10 AMAZE 80 becomes AMAZE
90 and is called.
FD 80 RT 90 This line is held in memory.
END
Each time Logo runs the procedure, it doesn’t get to the last
line. That’s because the procedure calls itself. So it writes
each last line on a “page” of the memory stack. It will keep
going, writing pages for each line it did not run — AMAZE
90, 100, 110, and finally 300. Then it stops.
FD 300, RT 90
FD 290, RT 90
FD 280, RT 90
…back to where she started…
FD 50, RT 90.
354
Recursion
TO TOWER :SIZE
IF :SIZE < 0 [STOP]
SQUARE :SIZE
TOWER :SIZE - 10
SQUARE :SIZE
FD :SIZE
END
TO SQUARE :SIZE
REPEAT 4 [FD :SIZE RT 90]
END
TO SQUARES :S
IF :S < 0 [STOP]
REPEAT 4 [FD :S RT 90]
FD :S
SQUARES :S - 5
END
TO TOWER :S :T
IF :T = 0 [STOP]
SQUARES :S
TOWER :S :T - 1
END
355
Recursion
They’re spirals.
No, you don’t need a
telescope to spy on
anything.
Spirals, squrials, polyspis, and fractals
draw some of the prettiest drawings you
can make using Logo.
FD 50 RT 90
FD 60 RT 90
FD 70 RT 90
FD 80 RT 90
FD 90 RT 90
…and on and on and on.
356
Recursion
357
Recursion
Does it help?
POLYGON 1 123 3
POLYGON 1 90 5
POLYGON 5 144 5
POLYGON 1 172 3
358
Recursion
________________________________________________
Multiple Spirals How would you put more than one spiral on the screen at
the same time?
Will the procedure at the top of the next page draw two
spirals?
359
Recursion
TO SPIRAL :N
IF :N > 100 [STOP]
FD :N RT 90
SPIRAL :N + 5
FD 200
IF :N > 100 [STOP]
FD :N RT 90
SPIRAL :N + 5
END
TO SPIRALS :N
SPIRAL :N
PU FD 200 PD
SPIRAL :N
END
360
Recursion
TO SPIRAL :N
IF :N > 100 [STOP]
FD :N RT 90
SPIRAL :N + 5
END
361
Recursion
Now try these. Can you predict what they’ll look like?
INSPI 10 0 15
INSPI 7 45 17
INSPI 10 4 20
INSPI 10 0 8
INSPI 3 45 30
Now try your own ideas. But before we move on, here’s
one more variation to explore.
362
Recursion
First, let’s start with a shoe box. Paint the inside of the
top using flat black paint. This creates a dull background to
show off your string patterns.
The next job is to create an even pattern that you will use
to punch tiny holes evenly around the edge of the box top. You
can do this very easily on the computer. Here’s a recursive
procedure that should be pretty easy for you by now.
TO MARK :DIST
RT 90 FD :DIST / 10
BK :DIST / 5 FD :DIST / 10 LT 90
END
363
Recursion
Print the patterns and cut them into narrow strips. Then
paste or tape them to the edge of your painted box top.
________________________________________________
Curves From Now there are lots of things you can do. For one thing,
Straight Lines you can use colored yarn and a needle to make curves from
straight lines.
364
Recursion
Soon you will have a pattern that looks like this, a curve
made from straight lines.
Turn one of those upside down and look what you’ll get.
365
Recursion
There are all sorts of patterns you can make. If you want
to dress them up a bit, try different colors of yarn for different
parts of the design.
When you’ve used up all your old shoe boxes, you can try
other designs on the Logo screen.
366
Recursion
367
Recursion
TO START1
PU HOME PD
END
368
Recursion
TO START2
PU HOME RT 180 FD 50 PD
END
________________________________________________
String and Wire Remember when you did some string and wire art earlier?
Art Procedures You never did get around to the Logo procedures, did you?
Guess what? You will now!
369
Recursion
BOXTOP draws the box top. You define the size of the
short side and the number of marks to appear on that side. For
example:
BOXTOP 300 30
TO MARKER :MARK
REPEAT :MARK [FD :DIST / :MARK MARKS]
END
370
Recursion
TO MARKS
LT 90 FD 5 BK 10 FD 5 RT 90
END
371
Recursion
TO HDG
SETH REMAINDER (:N * 5 * :DIST) 360
END
TO HDG1
SETH REMAINDER (5 * (:N-1)) 360
END
372
Recursion
Now let’s get to it. Just remember, the whole idea behind
Logo is to break a problem down into its simplest parts. Start
with what you know. Determine what you don’t know. Then
go find it.
You know that the three points of the triangle are going
to be on the edge, or the circumference of the circle. If you
can find a point that is the same distance from each of those
points, then you have the center of the circle, right?
373
Recursion
D
C
A
F
E
D
C
A
374
Recursion
OK, if you can find the point on line EF that makes these
two lines equal to line CD, you have found the middle of the
circle you want to draw.
Let’s do it.
________________________________________________
TO RANDOM.TRI
MAKE "POINTA POS
FD 100 RT 120 - RANDOM 30
MAKE POINTB POS
MAKE "DIST 250 - RANDOM 100 FD :DIST
MAKE "POINTC POS HOME
END
The turtle then goes FD between 150 and 250 and sets
POINTC. Then the turtle goes HOME. The next step is to
draw the perpendicular line.
TO RT.ANGLE
SETPOS LIST :POINTA
FD 100 / 2 RT 90
MAKE "POINTE POS
375
Recursion
FD 200 PU HOME PD
END
F
E
D
C
A
376
Recursion
TO CHECK.DIST
MAKE "BD DIST FIRST :POINTB LAST :POINTB ~
FIRST :POINTD LAST :POINTD
MAKE "CD DIST INT FIRST :POINTC ~
INT LAST :POINTC FIRST :POINTD ~
LAST :POINTD
TEST :BD = :CD
IFTRUE [HT CIRCLE :POINTD :BD]
IFFALSE [FD 1 MAKE "POINTD POS CHECK.DIST]
END
TO POS :LIST
OP LIST FIRST :LIST LAST :LIST
END
377
Recursion
378
Recursion
TO START
RANDOM.TRI
SETPOS :POINTA
FD 50 RT 90
MAKE "POINTD POS
CHECK.DIST
END
379
Recursion
380
Recursion
C Curves There are lots of books on fractals that you can read. So
rather than try to explain fractals, let’s look at how they work.
Here's the well-known C curve as a Logo procedure.
TO C :SIZE :LEVEL
IF :LEVEL = 0 [FD :SIZE STOP]
C :SIZE :LEVEL - 1 RT 90
C :SIZE :LEVEL - 1 LT 90
END
C 5 10
381
Recursion
C 20 3
________________________________________________
Automatic OK! Now add the SEE procedure on the next page. When
Startup you run SEE, you “see” how the turtle builds such complicated
pictures.
TO SEE
IF :LEVEL = 11 [STOP]
C :SIZE :LEVEL WAIT 50 CS
SEE :SIZE :LEVEL + 1
END
MAKE "SIZE 10
MAKE "LEVEL 0
382
Recursion
1. To run SEE when it’s first loaded, add the variables above
and this line:
CS C 10 1 WAIT 60
CS C 10 2 WAIT 60
CS C 10 3 WAIT 60
CS C 10 4 WAIT 60
CS C 10 8 WAIT 60
CS C 5 10 WAIT 60
383
Recursion
Figuring Out Now, where were we? Run the SEE procedure. You're
Fractals watching fractals in action.
384
Recursion
TO DRAGON 50 1
LDRAGON 50 1
END
TO LDRAGON
IF 1 = 0 [FD 50 STOP]
LDRAGON 50 1 - 1 LT 90
RDRAGON 50 1 - 1
END
385
Recursion
TO RDRAGON
IF 1 = 0 [FD 50 STOP]
LDRAGON 50 1 - 1 RT 90
RDRAGON 50 1 - 1
END
DRAGON 20 3
DRAGON 10 10
386
Recursion
Here’s a picture
from SNOWFLAKE
100 1
387
Recursion
TO START
CS PU SETPOS [-100 -100] PD
SETPC [0 0 255]
SNOWFLAKE 300 1 WAIT 30
SETPC [128 128 0]
SNOWFLAKE 300 2 WAIT 30
SETPC [128 0 0]
SNOWFLAKE 300 4
END
________________________________________________
388
Recursion
Taming the For some more complex drawings, take a look at the
Flicker Sierpinski gasket and carpet procedures that are on the CD
(SIERP.LGO). You’ve got recursive calls embedded all over
the place in these procedures.
These include the use of color, which can produce an
annoying screen flicker as they are being drawn. An easy way
to get rid of the flicker is to shut down the MSWLogo Screen
while the fractals are being drawn.
The CARPET procedure gives an example of how you do
this. Before the turtle starts drawing, enter the command
389
Recursion
TO TREE :LENGTH
IF :LENGTH < 2 [STOP] LT 45 FD :LENGTH
TREE :LENGTH / 2 BACK :LENGTH RT 90
FD :LENGTH
TREE :LENGTH / 2 BACK :LENGTH LT 45
END
390
Recursion
391
Recursion
392