Chapter 18 Em
-
Upload
cynthia-solomon -
Category
Documents
-
view
218 -
download
0
Transcript of Chapter 18 Em
-
7/31/2019 Chapter 18 Em
1/19
ms
-
7/31/2019 Chapter 18 Em
2/19
Chapter 18 Recursive Procedures
One of the most powerful features of Logo is thatyou can divide a project into procedures, each ofwhich is a distinct entity that has its own name. Aprocedure can be called (or used) by any other pro
cedure; it can also call other procedures. Someprocedures call on themselves; these proceduresare recursive. We have already used recursive pro
cedures. For example, p o l y and s p i are both re
cursively defined.
TO POL Y : ST EP : ANGLE
FD : STEP
RT : ANGLE
POLY : STEP : ANG LE This is the recursive call.
END
TO SP I : STEP : ANGLE : INC
FD : STEP
RT : ANGLE
SP I : STEP + : INC : ANGLE : INC
END
p o l y calls p o l y as part of its definition; and s p i calls s p i .
Lets think about this process. Imagine that Logohas an inexhaustible supply of helpers who arecomputer creatures living in the computer. Every
time a procedure is called a helper is called upon tolook up the definition of the procedure. The helperthen begins to carry out the instructions. It doesthis by calling on other helpers. Usually severalhelpers are needed to carry out one procedure.
For example, when p o l y is called its helper calls af d helper. After the f d helper finishes, a r t helperis called. When it finishes, a p o l y helper is called.The second p o l y helper calls a f d helper, a r t helper, and a p o l y helper. Meanwhile, the firstp o l y helper is still around waiting for the second
131
-
7/31/2019 Chapter 18 Em
3/19
p o l y helper to finish. In the process the f d helpers and the r t helpers finish their jobs (wedont know who they call for help). The p o l y helpers never finish; they keep on calling for newp o l y helpers.
When you use p o l y the process continues untilyou type c t r l -g . Not all recursive procedureswork this way. They can be made to stop. In fact,making up appropriate stop rules is a majorpart of writing recursive procedures.
Stopping Recursive ProceduresIn tills section we will discuss recursive proce
dures that stop. We will use as an example a proce
dure that counts backwards from a given number.Assume we have a procedure called c o u n t b a c k and we type
COUNTBACK 55
Logo will respond
-
7/31/2019 Chapter 18 Em
4/19
c o u n t b a c k for he lp , b u t th i s t im e g iv e i t i n u m
b e r - l a s in p u t.
COUNTBACK : NUMBER - 1END
Lets try it with a small number like 5.
COUNTBACK 5
Logo responds
5
43
21
0-1
-2
a n d s o o n u n t il y o u t y p e c t r l -g .
The procedure definition is almost complete. It
needs a stop condition. Lets make a stop rule for
c o u n t b a c k . For example, if the input is 0 then
stop.
I F : NUMBER = 0 [S TO P]
Now c o u n t b a c k looks like this:
TO COUNTBACK :NUMBER
IF :NUMBER = 0 [S TO P]
PR : NUMBER
COUNTBACK :NUMBER - 1
END
Try it by typing
COUNTBACK 5
-
7/31/2019 Chapter 18 Em
5/19
Logo responds
5
4
321
Everything stops. The last number that is printed
is 1.
Lets now think about this process in more de
tail and try to understand how Logos helpersinteract.
A Model for Recursion: Helpers
There is an inexhaustible supply of helpers readily
available to Logo. Each time a procedure is called,
whether it be p r i n t or m a k e or p o l y or c o u n t -
b a c k , a helper is put on the job. It looks up the
definition of the procedure and starts reading it.
The helper carries out what it can and then calls
other helpers as they are needed. For each
procedure call, a helper is put on the job.
Whenever a helper finishes its job it reports back
to whoever called it and goes away.
Lets revive the image of named things like :s i d e
or:s t a r t as contents of a container. Thus, if a pro
cedure takes an input the helper puts that input
in the container named on the title line. If some
thing is already there, the helper leaves it there,
and puts the new thing on top of it to be available
later. These containers can hold a lot. When the
helper has finished carrying out the procedure it
disposes of the input and then what was under it
in the container is now on top and accessible.
134
-
7/31/2019 Chapter 18 Em
6/19
I
Lets play computer by sketching out what thehelpers do. To do this give c o u n t b a c k a smallnumber as input. So
I COU NTB ACK 2
i Immediately a helper is called on and given the
procedure c o u n t b a c k with an input of 2. Letsdraw a diagram of the interaction.
i C O U N T B A C K 2 COUNTBACK INUMBER
I c o u n t b a c k 2 is called.i The inp ut , n u m b e r , is 2.
| COUNTBACK 2 stops ifNUMBER = 0.
| But NUMBERis 2.
| c o u n t b a c k 2 c o n t i n u e s ,
i 2 p r :n u m b e r
I COUNTBACK INUMBER 1
| c o u n t b a c k l i s ca l led by
COUNTBACK 2.
NUMBER is 1.
COUNTBACK 1 stops ifNUMBER = 0.
But NUMBER is 1.c o u n t b a c k l continues.
1 PR INUMBER
COUNTBACK INUMBER 1
c o u n t b a c k 0 i s ca l led by
COUNTBACK 1.
NUMBERis 0.
COUNTBACK 0 stops ifNUMBER = 0.So it stops.
c o u n t b a c k i now continues, but ithas finished its job and stops.
c o u n t b a c k 2 now continues, but it has? finished its job and stops.
135
-
7/31/2019 Chapter 18 Em
7/19
A helper stops when there is nothing more to do
or when s t o p is encountered.
Lets change the procedure so that the print action occurs in a different place. Make a new proce
dure, CB.
TO CB :NUMBER
I F : NUMBER = 0 [ S TO P ]
CB : N U M B E R - 1
PR : NUMBER
END
Type
CB 5
Logo responds
1
234
5
This is probably a surprise. But remember when
the first CB helper calls the second CB helper, it
waits for the second to do its job, and then the
first helper continues with its own job.
Lets trace through the process giving CB the
number 2 as input.
136
-
7/31/2019 Chapter 18 Em
8/19
CB 2 CB INUMBER
CB 2 is ca l led.
I
I
I
NUMBER is 2.CB 2 stops ifNUMBER = 0.
But n u m b e r is 2 so CB 2 continues.
CB INUMBER 1.
c b l i s c a l le d b y c b 2 .
NUMBER is 1.
CB 1 stops ifNUMBER is 0.
But n u m b e r is 1 so CB l helper continues.CB INUMBER 1
c b 0 is called b y c b l.
NUMBER is 0.
CB 0 stops ifNUMBER = 0.
So it stops.c b l continues.
PR INUMBER
c b l h a s f i n is h e d i t s j o b a n d s t o p s .c b 2 con t inues .
PR INUMBER
c b 2 now has finished its job and stops.
Note that the stop rule has to come before the re
cursion line. Otherwise, the recursion will go onforever and the stop rule will never be reached. Often, the stop rule will be the very first line in theprocedure.
There are other kinds of stop rules we could makeup. For example, in s p i
TO S P I : S T E P : A NG LE : I NC
FD : S TEP
RT : ANGLE
S P I : S T E P + : I NC : A N GL E : I NC
END
137
-
7/31/2019 Chapter 18 Em
9/19
We could decide that s p i should stop if :s t e p is
greater than 200. So, we include the line
I F : S T E P > 2 0 0 [ S T O P ]
After editing s p i it looks like
TO S P I : S T E P : A N GL E : I NC
I F : S T E P > 2 0 0 [ S T O P ]
FD : S T E P
R T : A N G L E
S P I : S T E P + : I NC : A N GL E : I NC
END
Try s p i . Ify o u d o n ' t l ik e t h e s t o p r u le , c h a n g e i t.
Designing a stop rule for p o l y can be a little trick
ier. p o l y completes a figure when the turtle re
turns to its starting state, which may require the
turtle to turn only one complete rotation, that is.
only 360 degrees. But sometimes the turtle turnsa multiple of 360 degrees.
Actually, we only need to know what the turtles
heading was when it started , and then com pare it
to the tu rtle s heading after each u i t t l So before
p o l y is called we have to find the tu rtle 's heading.
HF A
i h e n p o l y LOU UKI o see ii_!t_ ' -
i t 1 e*__.< 0 L Y Lo C j
-
7/31/2019 Chapter 18 Em
10/19
------
Now tr y p o l y .
There is a problem here. Each time you call p o l y you have to save the starting heading. It would bebetter to put that action in a procedure. Lets re
name p o l y and call it p o l y i . Then we can make anew p o l y which sets up :s t a r t and then runs
POLYI.
TO P O L Y : S T E P : ANG L E
P 0 L Y 1 : S T E P : AN G L E H E A D I N G
E N D
TO P 0 L Y 1 : S T E P : AN G L E : S T A R T
FD : S T E P
R T : A N G L E
I F H E A D I N G = : S T A RT [ S T O P ]
P 0 L Y 1 : S T E P : AN G L E : S T A R T
END
Now p o l y will do the whole job.
There are many designs you can create with s p i and p o l y . There are many ways of combiningpolygons to make new and dazzling designs.
This introduction to Logo is finished, but we hopeyou will explore other turtle projects on your own.
TheReference Manualdescribes other features ofLogo which you might want to try now.
Have fun.
-
7/31/2019 Chapter 18 Em
11/19
Chapter 18 Recursive Procedures
One of the most powerful features of Logo is thatyou can divide a project into procedures, each ofwhich is a distinct entity that has its own name. Aprocedure can be called (or used) by any other pro
cedure; it can also call other procedures. Someprocedures call on themselves; these proceduresare recursive. We have already used recursive pro
cedures. For example, p o l y and s p i are both re
cursively defined.
TO POL Y : ST EP : ANGLE
FD : STEP
RT : ANGLE
POLY : STEP : ANG LE This is the recursive call.
END
TO SP I : STEP : ANGLE : INC
FD : STEP
RT : ANGLE
SP I : STEP + : INC : ANGLE : INC
END
p o l y calls p o l y as part of its definition; and s p i calls s p i .
Lets think about this process. Imagine that Logohas an inexhaustible supply of helpers who arecomputer creatures living in the computer. Every
time a procedure is called a helper is called upon tolook up the definition of the procedure. The helperthen begins to carry out the instructions. It doesthis by calling on other helpers. Usually severalhelpers are needed to carry out one procedure.
For example, when p o l y is called its helper calls af d helper. After the f d helper finishes, a r t helperis called. When it finishes, a p o l y helper is called.The second p o l y helper calls a f d helper, a r t helper, and a p o l y helper. Meanwhile, the firstp o l y helper is still around waiting for the second
131
-
7/31/2019 Chapter 18 Em
12/19
p o l y helper to finish. In the process the f d helpers and the r t helpers finish their jobs (wedont know who they call for help). The p o l y helpers never finish; they keep on calling for newp o l y helpers.
When you use p o l y the process continues untilyou type c t r l -g . Not all recursive procedureswork this way. They can be made to stop. In fact,making up appropriate stop rules is a majorpart of writing recursive procedures.
Stopping Recursive ProceduresIn tills section we will discuss recursive proce
dures that stop. We will use as an example a proce
dure that counts backwards from a given number.Assume we have a procedure called c o u n t b a c k and we type
COUNTBACK 55
Logo will respond
-
7/31/2019 Chapter 18 Em
13/19
c o u n t b a c k for he lp , b u t th i s t im e g iv e i t i n u m
b e r - l a s in p u t.
COUNTBACK : NUMBER - 1END
Lets try it with a small number like 5.
COUNTBACK 5
Logo responds
5
43
21
0-1
-2
a n d s o o n u n t il y o u t y p e c t r l -g .
The procedure definition is almost complete. It
needs a stop condition. Lets make a stop rule for
c o u n t b a c k . For example, if the input is 0 then
stop.
I F : NUMBER = 0 [S TO P]
Now c o u n t b a c k looks like this:
TO COUNTBACK :NUMBER
IF :NUMBER = 0 [S TO P]
PR : NUMBER
COUNTBACK :NUMBER - 1
END
Try it by typing
COUNTBACK 5
-
7/31/2019 Chapter 18 Em
14/19
Logo responds
5
4
321
Everything stops. The last number that is printed
is 1.
Lets now think about this process in more de
tail and try to understand how Logos helpersinteract.
A Model for Recursion: Helpers
There is an inexhaustible supply of helpers readily
available to Logo. Each time a procedure is called,
whether it be p r i n t or m a k e or p o l y or c o u n t -
b a c k , a helper is put on the job. It looks up the
definition of the procedure and starts reading it.
The helper carries out what it can and then calls
other helpers as they are needed. For each
procedure call, a helper is put on the job.
Whenever a helper finishes its job it reports back
to whoever called it and goes away.
Lets revive the image of named things like :s i d e
or:s t a r t as contents of a container. Thus, if a pro
cedure takes an input the helper puts that input
in the container named on the title line. If some
thing is already there, the helper leaves it there,
and puts the new thing on top of it to be available
later. These containers can hold a lot. When the
helper has finished carrying out the procedure it
disposes of the input and then what was under it
in the container is now on top and accessible.
134
-
7/31/2019 Chapter 18 Em
15/19
I
Lets play computer by sketching out what thehelpers do. To do this give c o u n t b a c k a smallnumber as input. So
I COU NTB ACK 2
i Immediately a helper is called on and given the
procedure c o u n t b a c k with an input of 2. Letsdraw a diagram of the interaction.
i C O U N T B A C K 2 COUNTBACK INUMBER
I c o u n t b a c k 2 is called.i The inp ut , n u m b e r , is 2.
| COUNTBACK 2 stops ifNUMBER = 0.
| But NUMBERis 2.
| c o u n t b a c k 2 c o n t i n u e s ,
i 2 p r :n u m b e r
I COUNTBACK INUMBER 1
| c o u n t b a c k l i s ca l led by
COUNTBACK 2.
NUMBER is 1.
COUNTBACK 1 stops ifNUMBER = 0.
But NUMBER is 1.c o u n t b a c k l continues.
1 PR INUMBER
COUNTBACK INUMBER 1
c o u n t b a c k 0 i s ca l led by
COUNTBACK 1.
NUMBERis 0.
COUNTBACK 0 stops ifNUMBER = 0.So it stops.
c o u n t b a c k i now continues, but ithas finished its job and stops.
c o u n t b a c k 2 now continues, but it has? finished its job and stops.
135
-
7/31/2019 Chapter 18 Em
16/19
A helper stops when there is nothing more to do
or when s t o p is encountered.
Lets change the procedure so that the print action occurs in a different place. Make a new proce
dure, CB.
TO CB :NUMBER
I F : NUMBER = 0 [ S TO P ]
CB : N U M B E R - 1
PR : NUMBER
END
Type
CB 5
Logo responds
1
234
5
This is probably a surprise. But remember when
the first CB helper calls the second CB helper, it
waits for the second to do its job, and then the
first helper continues with its own job.
Lets trace through the process giving CB the
number 2 as input.
136
-
7/31/2019 Chapter 18 Em
17/19
CB 2 CB INUMBER
CB 2 is ca l led.
I
I
I
NUMBER is 2.CB 2 stops ifNUMBER = 0.
But n u m b e r is 2 so CB 2 continues.
CB INUMBER 1.
c b l i s c a l le d b y c b 2 .
NUMBER is 1.
CB 1 stops ifNUMBER is 0.
But n u m b e r is 1 so CB l helper continues.CB INUMBER 1
c b 0 is called b y c b l.
NUMBER is 0.
CB 0 stops ifNUMBER = 0.
So it stops.c b l continues.
PR INUMBER
c b l h a s f i n is h e d i t s j o b a n d s t o p s .c b 2 con t inues .
PR INUMBER
c b 2 now has finished its job and stops.
Note that the stop rule has to come before the re
cursion line. Otherwise, the recursion will go onforever and the stop rule will never be reached. Often, the stop rule will be the very first line in theprocedure.
There are other kinds of stop rules we could makeup. For example, in s p i
TO S P I : S T E P : A NG LE : I NC
FD : S TEP
RT : ANGLE
S P I : S T E P + : I NC : A N GL E : I NC
END
137
-
7/31/2019 Chapter 18 Em
18/19
We could decide that s p i should stop if :s t e p is
greater than 200. So, we include the line
I F : S T E P > 2 0 0 [ S T O P ]
After editing s p i it looks like
TO S P I : S T E P : A N GL E : I NC
I F : S T E P > 2 0 0 [ S T O P ]
FD : S T E P
R T : A N G L E
S P I : S T E P + : I NC : A N GL E : I NC
END
Try s p i . Ify o u d o n ' t l ik e t h e s t o p r u le , c h a n g e i t.
Designing a stop rule for p o l y can be a little trick
ier. p o l y completes a figure when the turtle re
turns to its starting state, which may require the
turtle to turn only one complete rotation, that is.
only 360 degrees. But sometimes the turtle turnsa multiple of 360 degrees.
Actually, we only need to know what the turtles
heading was when it started , and then com pare it
to the tu rtle s heading after each u i t t l So before
p o l y is called we have to find the tu rtle 's heading.
HF A
i h e n p o l y LOU UKI o see ii_!t_ ' -
i t 1 e*__.< 0 L Y Lo C j
-
7/31/2019 Chapter 18 Em
19/19
------
Now tr y p o l y .
There is a problem here. Each time you call p o l y you have to save the starting heading. It would bebetter to put that action in a procedure. Lets re
name p o l y and call it p o l y i . Then we can make anew p o l y which sets up :s t a r t and then runs
POLYI.
TO P O L Y : S T E P : ANG L E
P 0 L Y 1 : S T E P : AN G L E H E A D I N G
E N D
TO P 0 L Y 1 : S T E P : AN G L E : S T A R T
FD : S T E P
R T : A N G L E
I F H E A D I N G = : S T A RT [ S T O P ]
P 0 L Y 1 : S T E P : AN G L E : S T A R T
END
Now p o l y will do the whole job.
There are many designs you can create with s p i and p o l y . There are many ways of combiningpolygons to make new and dazzling designs.
This introduction to Logo is finished, but we hopeyou will explore other turtle projects on your own.
TheReference Manualdescribes other features ofLogo which you might want to try now.
Have fun.