9. The turtle is learning
If you managed to pass the lessons successfully so far, you might have seen that it is not that easy to edit the commands you give. To simplify to process you can teach Logo's Turtle a series of commands, a series of instructions. Teaching the turtle is simple. Write the command to followed by the word you want to teach the turtle and at the end write the line of commands for the turtle to learn. For example : to dashline repeat 5 [setwidth 1 fd 10 setwidth 3 fd 10] end. This command will teach the turtle a new word: dashline. (dashline = create a dashed line). Teach the turtle what is a dashline using the command line shown above.
to dashline repeat 5 [setwidth 1 fd 10 setwidth 3 fd 10] end
Now you will see that the turtle really does remember what he learned. To ask the turtle to draw a dashed line just repeat the new instruction word you taught him before. Draw a dashed line using the new instruction (the new word) you taught the turtle before.
Write only the instruction word itself.
You have drawn a dashed square before, but now it will be much easier. Use the word we taught the turtle before to draw a square. Draw a dashed line square by drawing 4 dashed lines and using the turning right command.
If you forgot how to draw a square, read the previous lessons.
repeat 4 [rt 90 dashline]
You might want to draw the same square again, therefore you can teach the turtle what is your favorite square. Ask the turtle to learn how to draw a square like the one you drew before.
Don't forget to end.
to square repeat 4 [rt 90 dashline] end
Now you can easily draw a dashed square using only one word. Clear the screen and draw again the same dashed square as before.
Use only two commands
This next strange symmetric figure we are going to draw will consist of 20 dashed squares. Between one square and another we will turn right at an angle of 360/20 = 18 degrees. Remember that the turtle already knows how to draw a dashed square. So all we have left to do is define the loop which will create the squares correctly. Clear the screen and draw twenty dashed squares.
cs repeat 20[rt 18 square]
You have already taught the turtle which is your favorite dashed line, but what if you'd like to draw a different dashed line? In that case you can teach the turtle the same word again, only this time it will be with different instructions. The turtle will only remember the very last commands he learned. Teach the turtle again what is a dashline. Use an instruction similar to the one we had at the beginning of the lesson, but now make the wider segments to be 6 points wide instead of 3.
to dashline repeat 5 [setwidth 1 fd 10 setwidth 6 fd 10] end
Why should we check how the new dashed line looks like when we can already check how a whole square made out of dash lines looks like? Ask the turtle to draw a square. Notice how he is different from the square the Logo turtle drew before, with the same simple instruction. Draw a square using the instruction we already taught the turtle.