Words that call themselves

Page 19

A while ago we drew a spiral with REPCOUNT. This is what the code looked like:

REPEAT 30 [FD REPCOUNT*5 RT 90]

Now let's draw a spiral in another way. We'll give it a name this time:

TO SPIRAL :size
   IF (:size < 10) [STOP] ; the stop condition
   FD :size RT 90 ; the action
   SPIRAL :size-10 ; the call
END

Why would you draw a spiral like that? Wasn't the first way simpler? Yes, but it's good to understand this way also. It is very powerful and can be used to draw many amazing things.

Let's look at what happens if we tell the turtle SPIRAL 100. First he sees this:

SPIRAL 100

That means he should write 100 on a piece of paper and put it in his :size pocket.

IF (:size < 10) [STOP]

This is "the stop condition". The turtle looks in the front of his :size pocket and sees 100. He asks himself if 10 < 100. If it is, then he will STOP. It isn't, so he carries on.

Now comes "the action". First FD :size, so the turtle walks FD 100. Then RT 90. Now he has drawn this:

First line in the spiral

And then "the call":

SPIRAL :size-10

How can we tell the turtle to do SPIRAL again before he's finished with the first? The turtle doesn't care. He just says: I'll finish the first SPIRAL later. He knows that the latest paper he put in his :size pocket is the only thing he needs to keep track of now. He saves the rest for later.

:size-10 is 100-10. That's 90. So the turtle now puts a paper with 90 in front of the other paper in his pocket. The next thing he sees is this:

IF (:size < 10) [STOP]

This is "the stop condition" again. He looks at the paper he just put in his :size pocket. It's 90, so it's larger than 10. Therefore he doesn't stop.

Then there's "the action". He sees FD :size. So he walks FD 90. Then RT 90. Now he has drawn this:

The two first lines in the spiral

Then there's "the call" again:

SPIRAL :size-10

He says: I'll finish this later. Now I must draw SPIRAL 80. He writes 80 on a paper and puts it in front of the other papers in his :size pocket.

Maybe you think that the turtle is just repeating himself now. But he doesn't care. He just does what he's told. And it looks like it works: he really is drawing a spiral.

Then we have "the stop condition" again:

IF (:size < 10) [STOP]

The topmost paper in his :size pocket says 80. And 80 > 10. So he doesn't stop. Then there's "the action" again. So he walks FD 80. Then RT 90. Now he has drawn this:

The three first lines in the spiral

Then there's "the call" again:

SPIRAL :size-10

So he thinks: I'll finish this later. Now I must draw SPIRAL 70. He writes 70 on a paper and puts in his pocket.

And that way it will keep going. The turtle will ask himself if :size < 10. If it is not he will will walk a little bit. Then he will turn. And then he will start drawing SPIRAL again with :size 10 less than before. Next time :size will be 60. Then 50. Then 40. 30, 20, 10. And finally 0.

What happens when :size is 0 and the turtle sees this?:

IF (:size < 10) [STOP]

He will see that 0 < 10. So he must STOP. He has finished, so he takes the topmost note in his pocket and throws it away. That note had the number 0 on it. So now the topmost note says 10. Then he goes back to what he was doing earlier. He's in SPIRAL 10 and the next instruction is END. So he throws away the paper with 10 on it too. The next note says 20. He's in SPIRAL 20 and the next instruction is END. And so on.

When he has thrown away the last piece of paper he stops. Doing things this way is called recursion. It's a good trick to learn. It's easy for turtles but adults find it difficult. Probably more difficult than you think it is. Adults are weird that way.

It is very important that you Think Turtle. Try to Think Turtle and do SPIRAL 45. It will take a while. But that's ok.

On the next page we'll draw my favorite picture in the book.

These are the words we know now. They are the same as on the last page:

Logo word What it means
FD 100 Walk forward 100 steps
BK 100 Walk backward 100 steps
LT 90 Turn left (good for drawing squares)
RT 90 Turn right (good for drawing squares)
LT 120 Turn left (good for drawing triangles)
RT 120 Turn right (good for drawing triangles)
CS Remove everything you have drawn
HOME Move back to the starting point (and draw a line)
PU Lift the pen so you won't draw when you walk
PD Put the pen towards the screen again
REPEAT 10 [       ] Repeat what's in the box 10 times
SETPC 4 Change the pen to pen number 4
TO ... END Remember the word after TO in the future
; Don't listen to me now
PRINT [I'm a turtle!] Say "I'm a turtle!"
RANDOM 10 Choose a number from 0 to 9
WAIT 60 Wait for 1 second.
PRINT 2+2 Makes the turtle say 4
TO SQUARE :top Put 5 in your :top pocket when I say SQUARE 5
REPCOUNT The number of the repetition
IF ( ) [ ] Is ( ) true? Then do [ ]
IFELSE ( ) [ ] [ ] Is ( ) true? Then do the first [ ]. Otherwise do the second [ ]

These are the words we have taught the turtle. SPIRAL is new:

New Logo word What it means
SQUARE :top Draw a square
CIRCLE :x :size Draw a circle
STAR :number :length Draw a star
LEAF Draw a leaf
BLOOM Draw a bloom
SPIRAL :size Draw a spiral

Turn page, or go back to the table of contents.