Introduction to Logo

Adapted with permission from a document by James R. King, copyight 1991.
 Mtht 480

Logo is an ideal programming language for young students.  It is a structured language, so the kids will learn good programming habits from the start.  It is very visual and provides immediate feedback, as a computer "turtle" walks around the screen in response to commands.  The version we are going to use is free and runs on most types of computer, but there are fancy fun versions of Logo available for those with more money (for example, check out Microworlds Logo.)

As you know, computers do exactly what you tell them to.  Learning to program will help your students learn to be precise in their thinking.  The computer cannot interpret a poorly phrased request, but will wait patiently for the student to decide what he or she really meant and enter a correct command.  Programming may be as useful as proof writing in encouraging logical, precise thinking.


In this handout, we go through a series of exercises to familiarize you with some basic Logo commands.  (If you already know Logo, some commands may be different from the ones you remember.  Versions of Logo differ, just like different web browsers or editors accept different commands.)  For starters, we will familiarize ourselves with the following commands. Some commands have a short form listed. As you become more comfortable with Logo, you will want to start using the shorthand versions of the commands.
 
Command Short Form
showturtle st
hideturtle ht
forward fd
back bk
right rt
left lt
clearscreen cs
penup pu
pendown pd
setpencolor setpc
help help
repeat repeat
to to
save save
edit edit
load load
 

Install UCBLogo or some equivalent program on your computer.  Start the program and move the windows around on the screen until you are comfortable; the one with the black background will hold the pictures you draw, the white one with the question mark prompt is where you enter your commands.
 

Showturtle and Hideturtle

The "turtle" is represented by a triangle in the center of the black screen. As the turtle "walks" on the screen, it will draw a line. To hide the turtle, type hideturtle in the white window and hit enter. To show the turtle again, type showturtle at the question mark prompt and hit enter.
 

Forward, Back, Right and Left

To draw pictures, you need to be able to move and steer the turtle. Enter the following commands and watch what happens:
forward 50
right 90
forward 100
The back and left commands work similarly. Try them out! Look up the short forms of these commands in the table above and see how they work. Try drawing a simple figure -- a square or triangle. (We'll draw more complicated pictures later. Be patient.)

Clearscreen

When you're tired of looking at your picture, or if you make a mistake, you can reset the turtle and background to their original state using the command clearscreen, abreviated cs.
 

Penup, Pendown and Setpencolor

The command penup (pu) stops the turtle from drawing as it moves. Pendown (pd) starts it again. (Logo is more fun than an Etch-a-sketch!)  Setpencolor (setpc) changes the color of the line the turtle draws. Color 0 is black, color 7 is white.  Try typing:
cs pu rt 90 fd 100 pd
setpc 1
rt 180
fd 100
rt 90
to see what color 1 is. (Notice that we can enter two or more commands on the same line, as we did here. It is good form to always leave the turtle in the position you found it; this will be more important later on.)

Help

If you know the name of a command, you can get a hint about how to use that command by entering:

help "commandname

at the ? prompt. If you don't know the name of the command, just type help and guess. Hitting enter scrolls through the help pages. Try entering:

help "setpc

(The quotes in front of the command name tell Logo to interpret the word after the quotes as a word and not a variable or procedure. If you leave out the quotes, Logo will try to "evaluate" the command, causing it to run a program and/or give you an error message.)


Now that we've learned the basics, we can play around a bit. If there's something you've been dying to try, do it now. When you're done, try entering the following commands.

cs fd 400

What happens to the turtle when it moves off the top of the screen?  Experiment with rt and fd to see what happens as it moves off the edge of the screen.

Now draw a square:

cs fd 50 rt 90 fd 50 rt 90 fd 50 rt 90 fd 50 rt 90

Can you draw a triangle?



 

Repeat

As you've no doubt noticed, you can quickly get tired of typing the same thing (and making the same typos) over and over. The repeat command solves that problem. Try this:

repeat 4 [fd 50 rt 90]

The repeat command performs the action in square brackets as many times as the number before the brackets. Like most computer languages, Logo is very fussy about syntax. You must use the right shape brackets and have spaces between the command, the number, and the brackets for this command to work.

Can you draw a triangle using repeat? A pentagon?

To

The line "repeat 4 [fd 50 rt 90]" is easier to type than "fd 50 rt 90 fd 50 rt 90 fd 50 rt 90 fd 50 rt 90", but it would be even better to have a simpler way of drawing a square. Part of the point of programming is to make the computer do repetitive jobs like this quickly and efficiently. For example, we could type:

repeat 6 [repeat 4 [fd 20 rt 90] pu fd 30 pd]

but it would be far more pleasant (and readable) to just say:

repeat 6 [square pu fd 30 pd]

Using the "to" command we can define a new Logo function to draw squares. Enter the following command at the ? prompt:

to square

Notice that when you hit enter, the prompt changes shape. This is the computer's way of telling you that it's waiting for more instructions on what it means to "square". Keep typing:

repeat 4 [fd 20 rt 90]
end
Once you've typed end, the computer goes back to the ? prompt; you have just told the computer that you're done defining the square command. (Note: It is a good idea to leave the turtle in the same position and orientation you started with when you define a new command. You should avoid using commands like cs or setpc when writing commands, because it will generally make them less useful to you in the future.)

Try typing square at the ? prompt. Clear the screen and try:

repeat 6 [square pu fd 30 pd]

much simpler! 

Save and Edit

Did you mistype your definition of the square command, or are you unhappy with how it works? You can change that, but first lets save our work! At the ? prompt, type:

save "filename

where filename is some unique name like heidislogo. By default, Logo saves the file in the program folder. You can move it to a floppy disk or delete it later. Notice that we have to use quotes again, to keep the computer from trying to evaluate our filename.

Now, type:

edit "square

at the ? prompt. A new edit window comes up. Make any changes to the square command that you feel are necessary. Then choose "Accept Editor Changes" from the Edit menu. This will return you to the Logo window, with a new improved definition of the command square.

Load

To load a Logo file, put that file in the Logo folder and type:

load "filename

This will load any functions defined in the file filename.

One great thing about these Logo files is that you can open them using SimpleText! You don't need to have a copy of UCBLogo on your computer to read your programs (but since it's free, you might as well have one!)

Functions with Arguments

The square function is very nice, but the side length is always the same. What if we want to be able to draw squares of different sizes? We have to give the computer a variable, and tell it how to use that variable. To do this, we include a word preceded by a : in the definition of the function. (The : serves the same purpose as the ". It keeps the computer from evaluating what we type after the symbol. In this case, it tells the computer that that word is to be interpreted as a variable.) Let's define a new function sq which will draw a square with a specified side length.
to sq :side
repeat 4 [fd :side rt 90]
end
Try it out!

sq 100
sq 50

You can also define functions that use two variables, and you can use functions within functions:

to twosquares :side :ratio
sq :side
sq :side * :ratio
end
You could use the function twosquares in class to discuss the relation between ratios of side lengths of squares and ratios of areas. For example, the command:

twosquares 100 1.5

 illustrates that increasing the side length of a square by half more than doubles its area!

Arithmetic in Logo

Note the use of multiplication in the last example; most mathematical symbols work just the way you expect them to in Logo. The symbols for sddition, subtraction, multiplication, and division are: +, -, *, and /. You can use parenthesis to specify which operations to perform first, as in (3+5)/4.


Exercises

    Write a Logo procedure to draw a
    Mtht 480