1. Logo Tutorial
History
Logo was developed in the late 1960s at Bolt Beranek and Newman, Inc., in
Cambridge, MA by W. Feurzeig, D. Bobrow and S. Papert. Its purpose was to teach
children to program. Its central feature is that it provides simple commands for
moving a ``turtle'' on a surface. Initially the language was used to direct the motion of
a small robot that was dressed up to look like a turtle. It was placed on a sheet of
paper and dragged a pen underneath it, thereby drawing figures on the paper. Today
the ``turtle'' is a small arrow-like figure that moves on the screen of a computer.
Underneath, Logo is very much like Lisp, the list-processing language used in
Artificial Intelligence, the branch of computer science that tries to make machines
behave intelligently. However, it also has features of other languages, including
Pascal.
The Logo Environment
The Logo in use at Brown is Berkeley Logo. It can be activated by selecting it from
the left Menu button. You will have to position a window on the screen. We will call
it the command window. Another window will appear, possibly on top of the
command window. If necessary, use the middle mouse button to move it (drag the top
of the window) so you can see the command window again. A ? prompt is presented
in the comand window. At the prompt, commands can be issued that are executed
immediately. When a drawing command is issued, say, the command forward 10, a
graphics window appears which must be positioned on the screen. The graphics
window in this case shows an arrowhead (the turtle) with a line behind it that is 10
units long. When other drawing commands are issued, such as rt 45 fd 60, the turtle
turns right 45 degrees and then advances another 60 units. The command cs will clear
the screen and reposition the turtle at its center.
Sometimes you will need to stop a Logo procedure. Do this with ^c (control c). To
exit logo, type bye in the command window.
Logo the Language
This tutorial provides the rudiments of the language, enough to do the simple
assignments that are given in CS4. The commands that we explain are drawing
2. commands, variables, arithmetic operators, and procedures, including recursive
procedures.
Drawing Commands
The simple Logo drawing commands move the turtle forward and backward and turn
it right or left. The commands and their abbreviations are given below:
fd forward
bk backward
rt right
lt left
cs clearscreen
Either version of these commands can be used.
Each of these commands must be followed by one value called its argument. The
arguments for fd and bk are units; those of rt and lt are angles which can be any
integer. Of course, a rotation by 360 is a complete rotation so a rotation by 375
degrees is the same as one by 15 degrees.
The graphics window has a coordinate system. The values of the two coordinates
(normally called x and y) at the center are 0, 0. At the northeast corner they are 250,
250; at the southeast corner, they are 250, -250; at the southwest corner, they are -250,
-250; etc. If the turtle tries to walk off one side of the screen, it wraps around. The
right side wraps to the left side and the top wraps to the bottom.
Now let's try some commands. Commands be issued one per line followed by a
carriage return. Several of them can be typed in succession in a command window
followed by a carriage return. The effect on the turtle is the same. However, if you
type a command that requires one or more inputs and provide the missing input(s) on
the next line, Logo will complain.
The commands
fd 60 rt 120 fd 60 rt 120 fd 60 rt 120
cause the turtle to draw a triangle, as you can see by trying them out. These
commands are read from left to right. Since the command fd requires one argument, it
is taken as the next value. Similarly, rt takes one argument also. Thus, Logo can give
unambiguous meaning to each of these character strings. For some Logo commands
separators are needed.
3. In the above example, the commands fd 60 rt 120 are repeated three times. To save
time and space Logo provides the repeat command. The following command has the
same effect as those given in the above example.
repeat 3 [fd 60 rt 120]
The square brackets indicate that the enclosed commands are to be executed three
times.
Other Turtle Moving and Drawing Commands
Logo has a number of other drawing commands, including those shown below.
pu penup
pd pendown
ht hideturtle
st showturtle
home
label
setxy
The pendown and penup commands tell the turtle to leave ink on the screen as it
moves or not leave ink, respectively. The hideturtle and showturtle commands hide or
show the turtle but do not affect its ability to leave ink as it moves. The home
command causes the turtle to return to the center of the screen. It may leave ink
behind when it does this. These four commands do not take arguments. The label
command takes a single word as a quoted string (e.g. ``a_string) or a list of words in []
brackets without quotation (e.g. [a string of letters]) and prints them on the graphics
window at the location of the turtle. (Try them!) The command setxy takes two
arguments, treats the first as the value of the abscissa (horizontal axis) and the second
as a value of the ordinate (vertical axis). It places the turtle at these coordinates,
possibly leaving ink as it does so.
What kind of figure does the following command sequence produce?
cs pu setxy -60 60 pd home rt 45 fd 85 lt 135 fd 120
Interpret these commands as you read them from left to right.
Logo Variables
A variable is the name of location that contains a value. In the random-access machine
each memory location has an integer address. Since it would be hard to remember the
address of each location containing a value used by a program, computer scientists
4. have found ways of given locations symbolic names. Once a variable has a name, we
can use and manipulate it.
Variables are given names which are strings of letters, hence size could be the name
of a variable. When we wish to use the value of a variable in a computation, we refer
to it as :size. Note the use of the colon before the name of the variable. For example, if
size has been given a value, then we can say fd :size and logo will move forward by a
number of units which is the value of the variable size.
There are several ways to give a value to a variable. An explicit way to do this is
described below. An implicit way will be seen when we introduce procedures. A
variable can be given a value with the make command, as shown below.
make "size 60
This command gives size the value 60. Note that in this case we have used ``size,
not :size. The reason is that :size is the value of the variable size, while ``size is its
name. We say that ``size is the ``quoted name'' of size. Logo tries to ``evaluate'' words
as it reads them because some words are the names of procedures. We quote words to
tell Logo they should be not be evaluated.
The make command gives a value to a variable even if it has not yet been used in a
program. In this way Logo differs markedly from Pascal. In Pascal a variable must
first be given a type (``declared'') such as integer, real, or character, before it can be
used. In Logo variables do not have types nor do they have to be declared before
being used.
As a digression, consider the command print. It is given one argument and it prints the
value of the argument in the command window. Thus, the command print 50 will print
the integer 50. We introduce this command so that we can demonstrate another use of
quoted names. The cleartext command, abbreviated ct, clears the text region of the
command window.
Since quotation before a word means it should not be evaluated, the command print
``hello should print the word hello in the command window. Does it?
Now let's give the variable first_programmer a value which is the name of the first
programmer. Try executing the following commands in the command window.
make "first_programmer "Ada_Lovelace
print :first_programmer
5. Note that we quote the name of the variable first_programmer in the make command
but use the colon version to obtain its value. Ada_Lovelace is quoted so it is
recognized as string and not a variable. (Ada Lovelace was the first programmer.)
Arithmetic Operations
Logo provides the usual arithmetic operations of addition, subtraction, multiplication
and division, denoted by the symbols +, -, *, /. Each of these operations produces a
result. If you don't do something with the result, Logo will complain. With the print
command the result of an arithmetic operation can be used and printed in the
command window. Try the following commands:
make "size 81/9
print 2*3
print :size - 4
Other useful commands are sqrt, which takes one non-negative argument and returns
its square root, power, which takes two arguments, call them a and b, and outputs a to
the b power, denoted ab, and ln, which takes one argument and returns its natural
logarithm. Other functions are exp, which takes one argument and computes e to that
power, e the natural number 2.718281828, and log10, which takes the logarithm to
base 10 of its one argument. For other operations, see the complete manual. Try ln
2.718281828. Whoops! Use print ln 2.718281828 instead.
Try out these ideas with the following simple code
make "angle 0
repeat 1000 [fd 3 rt :angle make "angle :angle + 7]
The figure it produces is shown below.
What are the successive values of the variable angle? How many blobs does it
produce? Suppose the integer 7 is changed to 11 or 13. How many blobs are produced
in these cases?
Arithmetic operators have precedences that determine the order with which they are
evaluated. Note that print 60 * sqrt 2 and print sqrt 2 * 60 produce different answers.
Here the * operator has precedence over the sqrt operator. Thus, * will be done before
sqrt if there is a choice, as there is in the second case. For this reason the first
statement prints the value of 60 times the square root of 2 whereas second prints the
square root of 120.
Randomization
Sometimes it is fun to have the outcome of a computation be unpredictable. Logo
provides the random procedure to generate a random number. random has one
6. argument and produces an integer value chosen uniformly at random between 0 and
the value of its argument. Thus, if you want a random angle between 1 and 360
degrees, you could use the command random 360 to produce it. Bear in mind that
Logo will complain unless you do something with the result, such as print it. Try
print random 360
several times in the command window and see what it produces. For a little more fun,
try
repeat 100 [fd random 80 rt 90]
repeat 1000 [fd 4 rt random 360]
The first procedure produces a drawing such as that shown below. What does the
second do?
Procedures
Procedures provide a way to encapsulate a collection of commands. Once a procedure
has been created, it can be used just the way a built in command is used. The
``meaning'' of a procedure is the meaning of its individual commands.
A procedure without arguments has the word to (a reserved word) and the name of the
procedure on the first line. (Reserved words in Logo cannot be used as variables and
have a well-defined meaning and use.) It has the reserved word end on the last line.
The procedure shown below encapsulates the next to the last set of instructions shown
above. Aft er writing it with the editor, invoke the procedure random_walk by typing
it in the command window.
to random_walk
repeat 100 [fd random 80 rt 90]
end
This is dramatic evidence that a procedure merely provides a name for a set of
commands.
Procedures can contain not only built in commands, they can also contain other
procedures. For example, if you want to build a tree (see the figure on the next page)
you will want to draw its trunk as well as its foliage. It is wise to begin experimenting
with a procedure to draw a trunk. When you are satisfied with the trunk procedure,
you can then construct a foliage procedure. A trunk procedure can be constructed
from procedures to draw the left side of the trunk, a second to draw the top, and a
third to draw the right side of the trunk. To finish up, it is prudent to have a procedure
to center the turtle on the top of the trunk. From this point a circle can be drawn for
the foliage. Through a series of experiments, we have constructed procedures of this
kind to draw the tree shown in the figure on the next page. The procedures producing
this drawing are given below.
to left_side
7. rt 20 fd 20 lt 20 fd 60
end
to top_side
rt 90 fd 25 rt 90
end
to right_side
fd 60 lt 20 fd 20 rt 20
end
to return_start
rt 90 fd 40
rt 90
end
to trunk
left_side
top_side
right_side
return_start
end
to center_top
pu
fd 80
8. rt 90
fd 20
lt 90
pd
end
to circle
repeat 360 [fd 1 rt 1]
end
to tree
pu bk 100 pd
trunk
center_top
left 90
circle
end
You are encouraged to experiment with these commands. Can you design a more
realistic foliage for the tree, perhaps by modifying the circle program so that it looks a
bit more ragged?
Editing Your Project
To edit your project, you will use the Xemacs editor, which starts up when you log in.
You can also start Xemacs from your left mouse button. Type your procedures into
your emacs window. When you are ready to try them out, select "Save Buffer" from
the "Files" menu, and save your file as /u/bridgexx/src-logo/Fractal/Fractal.logo,
where bridgexx is your bridge account number. Then, in your logo window, type load
"Fractal.logo to load your procedures into logo's memory. When you make changes to
your file in emacs, you will need to save it and re-load it into the command window.
9. Another, even stranger feature of the logo editor is that you cannot destroy procedures
once you have created them by deleting these procedures from the editor file!
Although it looks like your procedure is gone, it is still in logo's memory. To tell logo
to "un-know" that procedure, you need to type erase "procedurename in the command
window.
Note: Comments can be inserted on any line of a Logo program, after a semicolon.
Commenting programs is highly desirable at all times. The human mind is not able to
remember the details of complicated tasks and needs help. Also, without comments, a
person who is not the author of a program is likely to find it much more difficult to
understand and modify if the author did not properly comment it. See the fractal code
below for examples.
Procedure Invocation
There is a very simple rule to explain the meaning of a procedure that invokes other
procedures, including itself. It is called the copy rule. When one procedure calls
another it has the same effect as if the second procedure were copied into the first.
Control
The repeat command provides control over the number of times an operation is
performed. It has two arguments, the number of times to repeat a list and the list itself
in brackets []. We also have the stop command which stops the execution of a
procedure when it is reached. Another important control command is the if command.
It takes two arguments, a ``predicate'' and a list. A predicate is an expression that
evaluates to true or false. A predicate is constructed of two arguments and one of the
comparison operators <, > and =. Since they are ``infix operators'' they appear
between two numerical arguments, e.g. :size < 3. The following is a typical use of the
if command in our abbreviated introduction to Logo.
if :size < 3 [stop]
If the value of the variable size is less than 3, the procedure stops.
Procedures with Parameters
Parameters make procedures much more useful. Following the procedure name on the
first line any number of variables can be specified in the colon format, as illustrated
below:
to square :size
repeat 4 [fd :size rt 90]
end
This procedure draws a square. The length of each side is size. This procedure is
invoked by providing the procedure name and one parameter where the parameter is
10. either an integer or an integer-valued variable, e.g. square 60 or square :side where
:side is an integer-valued variable.
The three procedure given below draw a crude house. The procedure house invokes
the procedures square and floor.
to square :size
repeat 4 [fd :size rt 90] ; where is the turtle when this step
completes?
end
to floor :size
repeat 2 [fd :size rt 90 fd :size * 2 rt 90]
end
to house
floor 60 fd 60 floor 60 ; where is the turtle at this point?
pu fd 20 rt 90 fd 20 lt 90 pd
square 20
pu rt 90 fd 60 lt 90 pd
square 20
end
Recursive Procedures
When a procedure invokes a copy of itself, it said to be recursive. The meaning of
recursive procedures is obtained in exactly the same way as regular procedural
invocation, namely, via the copy rule. An example of a recursive procedure is given
below. BEWARE. This procedure will run indefinitely. You will need to remember
how to stop a running procedure from the command window, namely, with ^c on
Unix machines (Command . on Macs).
to star to walk_the_stars
repeat 5 [fd 10 rt 144] fd 20 rt random 360
end
star
walk_the_stars
end
Experiment with the procedure star to see what it does. The procedure walk_the_stars
moves 20 units, turns right by a random angle between 1 and 360, draws the star, and
then calls itself. By the copy rule, it will repeat these operations indefinitely, that is, it
never stops. It is for this reason that you must know how to stop Logo. This procedure
does not tell Logo to stop.
Another illustration of recursion is given below. In this case, the procedure fractal will
terminate. It is shown alongside a procedure to draw a triangle. If you look closely,
11. you will see that is formed from the triangle procedure by drawing a copy of a smaller
version of itself along each side of the triangle. Such figures are called fractals
because they have the self-similar property; they look the same at every degree of
magnification. (In this case, the magnification cannot be unlimited because the figure
is finite.) This particular fractal is called Serpienski's Gasket. It is very pretty.
to triangle :size
to fractal :size
repeat 3 [fd :size rt 120]
if :size < 3 [stop] ; the procedure stops if size is too small
end
repeat 3 [fd :size fractal :size/2 fd :size rt 120]
end
To run fractal, type fractal 60 (or fractal x for some other integer x) in the command
window.
Logo Tutorial
Telling Logo Where To Go
Getting Logo to move around is easy, just tell him where to go and he'll do it. Logo
always looks in a special place for instructions. That place is the first line inside
the main procedure. Press the Load Code button to load the code provided for you into
the pane on the right side of your Logo Application window. At the top of your screen
you'll see this:
procedure main
end
Put the instructions you want Logo to follow below procedure main and above end.
To get started, you'll need just three simple instructions. The first instruction, go, is
your way of telling Logo to walk forward in the direction he's facing. To get Logo to
walk forward 50 steps, type go 50. Remember to type this inside the main procedure.
Press the draw button and watch Logo walk forward 50 steps.
12. The other two instructions, rt and lt tell Logo which way to turn and how much. For
example, to make Logo turn right 90 degrees, type rt 90. If you don't remember how
degrees work, take a look at this explanation.
The Maze
Your first challenge is to get Logo out of the maze. Use the Load Maze button on your
LogoApplication to draw the maze. Now use the instructions you just learned, go, rt,
and lt to get Logo out of the maze. Let us know if you need help or hints.
Animation
Logo moves so fast that you really can't see him move, you can only tell where he's
been by the line he draws as he moves. To slow Logo down enough that you can see
him moving, we wrote a procedure, logo. It is one of the procedures that was loaded
when you pressed the Load Code button. You can use logo instead ofgo to tell Logo to
move slowly. To tell Logo to take 50 steps, slowly, type goto logo, steps = 50.
Unfortunately, on some of your computers the screen will flicker a lot when Logo
moves slowly. What can we say, animation is tricky.
Syntax
Some of you may have experienced problems by now. If you forgot to put your
instructions inside the main procedure then you got an error. Also, every computer
language requires you to follow very precise rules when writing instructions. These
rules are known as the syntax of the language. If you do not follow these
rulesprecisely Logo will not be able to underestand your instructions.
So far, you've used four Logo commands. The first three commands, go, rt and lt just
tell Logo how to move. The fourth, goto, allows you to call a procedure.
Calling Procedures
The syntax for calling procedures is:
goto procedure name, parameter = value
In goto logo, steps = 50
• procedure name is logo
• parameter is steps
13. • value is 50.
Some procedures have no parameters, so the syntax for calling them is just:
goto procedure name
Other procedures have two parameters, and the syntax for calling them is:
goto procedure name, parameter1 = value1, parameter2 = value2
Can you figure out what the syntax for calling a procedure that has three parameters
would be?
Write Your Own Procedure
You can make procedures of your own if you want. The syntax for any procedure is
the same as that for the main procedure:
procedure procedure name
procedure body
end
In the main procedure you just wrote,
• procedure name is main
• procedure body is the code you wrote to get Logo out of the maze
Do not call your procedure main; main is a special word reserved for the main
procedure.
Try writing a procedure to draw a simple shape: a square or a triangle or a rectangle.
This is an example of a procedure to draw a pentagon (a five-sided figure).
procedure pentagon
goto logo, steps = 50
rt 360/5
goto logo, steps = 50
rt 360/5
goto logo, steps = 50
rt 360/5
goto logo, steps = 50
rt 360/5
goto logo, steps = 50
rt 360/5
end
14. Notice that Logo can do simple math; instead of bothering to calculate 360/5 for
myself, I let Logo do it.Also, I used the logo procedure because I wanted Logo to
move slowly. If you want Logo to move rapidly, use the go command instead. Go
ahead and call your procedure inside your main method. Use the Clear button to get
rid of the maze.
Parameterize Your Procedure
The pentagon procedure is nice, but it could be better. Right now, it only makes one
size of pentagon, one where Logo takes 50 steps on each side. If I make the number of
steps that Logo takes a parameter of the procedure, then I can use the same procedure
to draw pentagons of any size.
procedure pentagon
goto logo, steps = side
rt 360/5
goto logo, steps = side
rt 360/5
goto logo, steps = side
rt 360/5
goto logo, steps = side
rt 360/5
goto logo, steps = side
rt 360/5
end
The new procedure uses the variable, side for the number of steps, instead of the
number 50. Now, if I want Logo to draw a really small pentagon, I call pentagonlike
this: goto pentagon, side = 10. If I want Logo to draw a really large pentagon, I
call pentagon like this: goto pentagon, side=200. Of course, if Logo tries do draw a
pentagon that big, he might walk right off the drawing space. Can you parameterize
your procedure in the same way?
More Logo Instructions
Logo commands are the fundamental building blocks of the Logo language. So far,
you've used just four commands, go, rt, and lt and the goto command for calling a
procedure. Press the Logo Syntax button in LogoApplication to see the other
commands you can use.
The repeat command is very powerful. You can use this command to call a procedure
many times. These two procedures use repeat to draw a spiral. These procedures were
15. also loaded when you pressed the Load Code button. Try calling spiral from your
main procedure. Feel free to change spiral orspiralhelper and see how that changes
what Logo does.
procedure spiralhelper
goto logo, steps = loop
rt 50
end
procedure spiral
repeat 100, spiralhelper
end
Finishing Up
Do you want to save a procedure you wrote so you can use it later? To do that, open
the Notepad application on your computer, copy the procedure into a Notepad
document, and save it to the EYH folder on your Desktop. Name the
file your_name_here.logo. We'll post your procedure on the main page so that you can
access it anytime you want.