This tutorial is designed to introduce you to some basic LOGO commands as well as two fundamental and powerful principles in computer science: abstraction and parameterization. To get familiar with the ideas it is important that you go beyond merely typing in the commands from the tutorial. To get a deeper understanding and exercise your creativity muscles, you must dare to experiment on your own once you start to get the idea. Play. Explore, Discover. Make connections. Finally, save some images of your creations to put into the web page we will be making on Saturday.
For this exercise we will use a version of LOGO that can be run within a web browser. It is called Turtle Tracks. To run Turtle tracks in your browser right-click your mouse on this applet link and from the menu that pops up select "Open Link in New Window". This will open a new browser window with a web page containing a button labeled "Start". Click this button now to start Turtle Tracks.
The Turtle is an imaginary creature or robot which we can use to make line drawings on the screen by giving it commands. It traces a line along its path with a pen. First, we need a window to draw in.
To open a small window with a turtle click in the Turtle Tracks console window in the blank space under the words "enter command:" type draw. You should now see a small window with a black background and a little white triangle representing the turtle facing upwards.
? draw
To draw a line tell the turtle how far you want it to move. The distance is measured in "pixels" which are the tiny dots that form the image on your screen. Try forward 100 to command the turtle to move forward by 100 pixels. This should give you an idea of the how far 100 pixels is.
? forward 100
The turtle understands abbreviations for many of the commands. The command forward can be abbreviated
if at any time you want to erase the screen and start over type clearscreen or just cs for short.
You can tell the turtle to turn left or right by a certain number of degrees for example:
? right 45 ? left 45
These commands can be abbreviated too: rt is short for right and ``lt is short for left.
Tell the turtle to turn right 90 degrees and go forward another 100 steps (pixels):
? right 90 ? forward 100
It is OK to give the turtle more than one command on the same line:
? right 90 forward 100
Or the abbreviated commands:
? rt 90 fd 100
Know you all you need to be able to ask the turtle to draw a complete square. Go a ahead and try it before moving on. Remember you can type cs to clear the screen and start over. Or you can type clean to erase the lines the turtle has drawn but leave the turtle where it is.
We reduce the amount of typing a bit by using the repeat command:
? cs ? repeat 4 [forward 100 right 90]
The 4 tells the turtle how many times to repeat the sequence of commands that follows in the square brackets.
In computer science this is called "looping" because the turtle (that is the computer) "loops back" to the beginning of the commands. The whole line of commands above is called a "loop".
Try using the repeat command to make loops to draw some other shapes like a triangle, a hexagon, and a pentagon. Try this one out:
? repeat 24 [forward 150 right 165]
A very important skill needed to understanding patterns and describe them is the ability to make abstractions. It is the ability to recognize commonalities in more than one place or time and give the commonality a name, or symbol. Our text quotes R. Buckminster Fuller as saying "The wave is not the water. The water merely told us about the wave moving by." He is making the point that "wave" is an abstraction that is separate from the water. "Wave" is a symbol (abstraction) for a pattern of behavior of the water. The number 2 is a symbol representing an abstraction for certain a quantity of "things" where "things" might be objects, events, or other abstractions like thought, feelings or ideas. If we saw to pencils on the table or a bell ring twice, these would seem unrelated. One is a set of objects. The other is a set of events, but the common feature is that there are two of each. The concept "two" or "pair" is very abstract because it is not a physical thing itself, but it can describe nearly anything that comes in pairs.
That was a trivial example but the process of abstraction--recognizing the similarity and giving it a name, is fundamental to how we understand the world and make sense of it. In effect the only thing our thinking mind knows how to do is recognize patterns, which is really the same as forming an abstractions, and reason about those abstractions. In fact "square" is a an abstraction for forms which have certain characteristics: 4 straight sides of equal length connected by 90 degree angles.
The ability to define abstraction is critical to making a computer language that is flexible enough to tackle real problems and to allow us to adapt the instructions to something that matches how we think a bit better. In LOGO, abstractions are called "procedures". Here is how to teach the LOGO interpreter the "square" abstraction by defining a new procedure:
? to square > repeat 4 [forward 100 right 90] > end square defined.
Note that the '>' symbol lets us know we are in the middle of defining a new procedure. When we are done with the definition the command end marks the end of the definition and we are back to normal ready to give the turtle more commands. "square defined." was printed by the LOGO to let us know we were successful. Otherwise an error message would appear and we would need to start over. However, notice that pressing the up arrow key on the keyboard recalls previous commands.
Now we have created a new command which we can give the turtle. Try it out:
? square
Try making your own procedure for another shape like the star from step 5 above. Try making a circle procedure.
The process of abstraction involves "generalization". The square our procedure makes is always 100 steps per side. We might like to be able to generalize our procedure to allow us to create a more general class of squares by giving it the size we want each side to be. Then we would be able to do the following to make a smaller square:
? square 50
We can redefine our square procedure to make it accept the length of the sides by adding the length as a "parameter". We have already seen parameters. The forward 100 command has a parameter representing the number of steps with a value of 100.
Here is how to add a parameter named "length" to our square procedure.
To edit the square procedure you can type:
edit "square
Note that the quotes before square are necessary to tell LOGO we want it to treat square as a name rather than its normal behavior which would be to draw a square!
Now a new window with our procedure definition in it should appear.
Change the definition to look like the following:
to square :length repeat 4 [forward :length right 90] end
The colon ':' before length marks it as a parameter.
Now close the editor window and a dialog box will pop up asking what to do. Click the "Execute" button which will cause the interpreter to execute the new procedure definition. No square has been drawn yet, we only redefined the square procedure. Try typing:
cs square 50
You should see a smaller square. Experiment with a few more squares
You can also redefine a procedure without using the editor by typing the new definition directly, however you must erase the old definition. LOGO won't let you try to define to procedures with the same name. Here is the command:
eraseprocedure "square
which can be abbreviated to "erp "square". Again, the quotes are necessary to prevent the LOGO from executing the square command.
Now you should try out your own ideas. To give a little more fuel for new ideas here are some other things to try. This is the most important part of the exercise. Make some good nice images and save them for Saturday's web page tutorial.
If you made a circle procedure try generalizing it to take a radius as a parameter.
Try making a general polygon procedure that takes two parameters: the length of a side and the number of sides.
cs repeat 10 [square 100 right 36] cs repeat 24 [square 100 right 15]
Put that into a procedure:
to flower repeat 24 [square 100 right 15] end
Parameterize it:
to flower :reps repeat :reps [square 100 right (360 / :reps) ] end flower 10 flower 24
Here's another interesting procedure to experiment with:
to spiral :size :angle if :size > 100 [stop] forward :size right :angle spiral :size + 2 :angle end spiral 0 90 cs spiral 0 91
Notice that this procedure calls itself! In computer science this pattern of a procedure calling itself in its definition is called recursion.
A complete list of the turtle drawing commands can be found in the Turtle Tracks Documentation.