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.
Sometimes you will need to stop a Logo procedure. Do this with ^c (control c). To exit logo, type bye in the command window.
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.
fd 60 rt 120 fd 60 rt 120 fd 60 rt 120cause 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.
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.
Logo has a number of other drawing commands, including those shown below.
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.
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 60This 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
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.)
make "size 81/9 print 2*3 print :size - 4Other 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.
print random 360several 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?
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] endThis 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 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 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
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.
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.
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
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 endExperiment 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, 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] endTo run fractal, type fractal 60 (or fractal x for some other integer x) in the command window.