Download - Chapter 4 Em
-
7/31/2019 Chapter 4 Em
1/10
A First Procedure: Teaching theTurtle to Draw a Square
-
7/31/2019 Chapter 4 Em
2/10
Chapter 4 A First Procedure: Teaching the Turtleto Draw a Square
Using the commands f o r w a r d and r i g h t or
l e f t we can make the turtle draw a square. For
convenience we will often use the short names for
commands.
FD 3 0
RT 9 0
FD 3 0
RT 9 0
FD 3 0
RT 9 0
FD 3 0
RT 9 0
SQUARE
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 thesame 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
-
7/31/2019 Chapter 4 Em
3/10
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. Yourturtle 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 i t 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.
E D I T " S Q U A R E 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 BoxIf you do not remember to prefix s q u a r e with
a " (quote mark) and type
ED IT SQUARE
-
7/31/2019 Chapter 4 Em
4/10
v
I
r
Logo will respond
I DON'T KNOW HOW TO SQUARE
LOGO FfllTflR
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 SQUARE This is the title line.
t o informs Logo that the followingtext is part of a proceduredefinition.
s q u a r e is the name of theprocedure. You are free to chooseanother name.
The! (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 30
RT 90
FD 30
RT 90
FD 30
RT 90
FD 30
RT 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
-
7/31/2019 Chapter 4 Em
5/10
If you want to move the I (cursor) backwards in
the stream, use c t r l - b . Y ou 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 previous line, use c t r l - b to move the I (cursor) back
wards to where the bug is. When the I (cursor)
passes 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
FORWARD 30
R IGHT 90
becomes
FORWARD 30RIGHT 90
Press the r e t u r n key to separate the lines again.
FORWARD 30
R IGHT 90
If you had pressed space instead of the r e t u r n
key then you would see
FORWARD 30 RI GH T 90
30
-
7/31/2019 Chapter 4 Em
6/10
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.
SQUARE
REPEAT 3 [RT 45 SQUARE]
REPEAT 3 [RT 45 SQUARE]
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 DEF INED
Using the New Command
Try your new command. Type
SQUARE
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
RIGHT 45
and now type
SQUARE
Continue to repeat these two commands ( r t 45
and s q u a r e ) . To 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,
REP EAT 3 [RT 45 SQUARE]
31
-
7/31/2019 Chapter 4 Em
7/10
The first input indicates how many times to re
peat the enveloped instructions. The second inputis a list of instructions. The instructions must be
enclosed in brackets. Think of the brackets asmaking an envelope. Complete the design bytyping
RE PE AT 3 CRT 45 SQUARE ]
Now l e t s m a k e a p r o c e d u r e fo r th i s d e s i g n a n d Call it SQUARE STAR
E D I T " S Q U A R E S T A R
You will now be using the editor and the title linewill 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 linepress the r e t u r n key and type
REPEAT 8 [SQUARE RT 45]
END
Dont forget, you type c t r l -c when you are fin
ished editing.
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. Rememberthat CS will do this. Type
CS
and thenSQUARESTAR
32
-
7/31/2019 Chapter 4 Em
8/10
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 SCRUNCH
Logo prints a number which is the current set
ting. Now try
SETSCRUNCH .8
Then type
CS SQUARE
If your square looks worse try
SETSCRUNCH 1
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
example, there are many designs which use
s q u a r e . Some more examples are:
cu
FLAG CROSS FLAGS MANYFLAGS
-
7/31/2019 Chapter 4 Em
9/10
FLAG
CROSS
FLAGBACK
FLAGS
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 turtlewith 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.
f l a 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 FLAG
FD 30
SQUARE
END
TO CROSSR E P E A T 4
END
[FLAG RT 90 ]
TO FLAGBACK
FLAG
BK 30
END
TO FLAGS
R E P E A T 4
END
[F L A GBA C K R T 90 ]
MANYFLAGS Uses FLAGS.
TO MANYFLAGS
FLAGS
RT 45
FLAGSEND
34
-
7/31/2019 Chapter 4 Em
10/10
Logo Vocabulary
Full Name
REPEAT
SETSCRUNCH
SCRUNCH
CLEARSCREEN
SHOWTURTLE
FORWARD
RIGHT
BACK
LEFT
TO
END
EDIT
PRINT
Special Keys
CTRL-G
CTRL-B
CTRL-F
RETURN
Short Name
cs
ST
FD
RT
BK
LT
ED
PR
CTRL-C