Chapter 4 em
Chapter 4 em
Chapter 4
FD RT FD RT
30 90 30 90
FD RT FD RT
30 90 30 90
If we used 50 instead of 30, the turtle would draw a bigger square. We could use any number. Lets define a new command in Logo to get the turtle to draw a square. New commands in Logo are programs or procedures written by you. Each time you want a square you can use your new pro cedure rather than retyping the individual in structions. To define a new command you should first choose a name. Lets use s q u a r e because that seems natural. But any name will do. You could then use the Logo command t o and define s q u a r e in the same way as you did g r e e t . That is, you could type t o s q u a r e , then the in structions, then e n d . This method is good if you
SQ UA RE
are careful about typing and you know just what you want to type in advance. Introducing the Logo Editor There is another way to define a procedure. You could use the Logo editor. Then, if you make typ ing mistakes you can remove them easily. When you are using the editor, Logo carries out only ed iting actions. There is a disadvantage to using the editor. Your turtle drawings will be replaced by the editing screen and the picture will be lost. On the other hand, the disadvantage of using t o to define your new procedure is that you will not be able to fix typing errors except on the line where the I (cursor) rests.
EDIT e d it
or e d signals Logo that you want to edit. Follow it by the name of the procedure you want to edit. You must prefix the name with a " (quote mark). Do not type a space between " and the name of the procedure.
EDIT "SQUARE
Remember the
"
(quote mark).
After you press the r e t u r n key you will be using the Logo editor and only editing actions will be carried out. Bug Box If you do not remember to prefix a " (quote mark) and type
EDIT SQUARE
sq u a re
with
If you had turtle drawings on the screen they will go away when you start editing. When the editor starts up, the title line of the pro cedure will appear at the top of the screen.
TO S Q U A R E
This is the title line.
to
LOGO FfllTflR
s q u a r e is the name of the procedure. You are free to choose another name.
T he! (cursor) is at the end of the title line. Notice that Logo does not print ? or any other prompt symbol while you are using the editor. Since you do not want to change the title line press the r e t u r n key. Now type in the commands that make up s q u a r e . They are the commands you used previously.
FD RT FD RT FD RT FD RT 30 90 30 90 30 90 30 90
Think of everything you type in the editor as a stream of characters. If you want to add some thing to the stream, move the I (cursor) to that place.
29
If you want to move the I (cursor) backwards in the stream, use c t r l - b . Y o u can move all the way to the beginning of the stream. If you want to move the I (cursor) forward, use
CTRL-F.
Then type the characters you want and they will become part of the stream. Thus, if you have made a typing error in a pre vious line, use c t r l - b to move the I (cursor) back wards to where the bug is. When the I (cursor) p asses over the characters they remain un changed. If you want to erase a character the - key will erase the one to the left of the I (cursor) and will move the I (cursor) to that position. Notice if th e! (cursor) is at the beginning of a text line, pressing the - key will move it along with the entire line of text to the end of the previous text line. Thus
F ORWARD 3 0 RIGHT 90
becomes
FORWARD 3 0 R I G H T 90
Press the r e
tu rn
FORWARD 3 0 RIGHT 90
If you had pressed space instead of the key then you would see
FORWARD 3 0 RIGHT 90
re tu rn
30
You can move the I (cursor) from the last charac ter typed to the t in t o on the title line by typing c t r l - b several times. Type c t r l - c when you have completed your edit ing. If you do not type e n d , Logo will insert the word when you type c t r l - c . Logo will now end the procedure definition and carry out the t o command by defining s q u a r e . Logo will type
SQUARE DEFINED
Again, type
SQUARE
This time the turtle just retraced its path. If you turn the turtle left or right and then type s q u a r e again, a new drawing will appear. For ex ample, tell the turtle
SQ U A RE
RIGHT
45
R E P E A T 3 [RT 45 SQ U A RE]
Continue to repeat these two commands ( r t 45 and s q u a r e ) . T o do this you could use the Logo command, r e p e a t , r e p e a t requires two inputs. For example,
REPEAT 3 [RT 45 SQUARE]
R E P E A T 3 [RT 45 SQ U A RE]
31
The first input indicates how many times to re peat the enveloped instructions. The second input is a list of instructions. The instructions must be enclosed in brackets. Think of the brackets as making an envelope. Complete the design by typing
REPEAT 3 CRT 45 SQUARE]
Now
EDIT
l e t s m a k e a p r o c e d u r e fo r t h i s d e s ig n a n d
You will now be using the editor and the title line will be displayed on the screen.
TO SQUARESTAR
Notice that th e! (cursor) is at the end of the title line. Since you do not want to change the title line press the r e t u r n key and type
REPEAT END 8 [SQUARE RT 45]
c t r l
-c
It is always a good idea to try out your new proce dure. Put the turtle in its startup state in the cen ter of the screen facing straight up. Remember that CS will do this. Type
CS
and then
SQUARESTAR
32
Bug Box If your squares look like rectangles the bug is in your TV, and not in Logo. The Logo command s e t s c r u n c h allows you to change the aspect ra tio of the screen. If you want to know what the current setting is type
PR S C R U NC H
Logo prints a number which is the current set ting. Now try
SETSCRUNCH .8
Then type
CS SQUARE
Try other settings until you are satisfied. Other Uses o f s q u a r e Once you have defined a procedure, you can use it as you would any Logo primitive such as f d , b k , l t , r t , etc. Thus, a procedure you define can be used as part of the definition of other procedures. This is one of the powerful features of Logo. For exam ple, there are many designs which use s q u a r e . Some more examples are:
c u
FLAG CROSS FLAGS MANYFLAGS
Both f l a g and f l a g b a c k make the turtle draw the same design, but they leave the turtle in dif ferent states. Both procedures leave the turtle with the same heading as they found it, but f l a g leaves the turtle in a different position from the one it started in.
fla g b a c k ,
on the other hand, leaves the turtle in the same position on the screen as it was found. We can see the effect of these differences in c r o s s and f l a g s , c r o s s runs f l a g four times while f l a g s runs f l a g b a c k four times.
TO F L A G FD 3 0 SQUARE END TO C R O S S REPEAT 4 END [FLAG RT 9 0 ]
FLA G
CROSS
FLA G B A C K
M A NYFLA GS U s e s FLA G S.
TO M A N Y F L A G S FLAGS RT 4 5 FLAGS END
FL A G S
M A N Y FL A G S
34
Short Name
cs
ST FD RT BK LT
ED PR
Special Keys
CTRL-G CTRL-B CTRL-F R ETU RN CTRL-C