Upload
elaine-jennings
View
219
Download
4
Embed Size (px)
Citation preview
Extending Karel’s Vocabulary
This PPT originated with Dr. Judy HankinsModifications have been done by Dr. Untch
& Dr. Cripps
Why should we extend Karel’s vocabulary?
• Karel’s vocabulary is limited.
• For example, Karel does not understand a TurnRight(); command.
He can only turn right by executing three TurnLeft(); commands.
• As a second example, suppose we need to program Karel to travel over vast distances.
• Assume that the robot must move east one mile (a mile is eight blocks long), pick up a beeper, and then move another one mile north.
• Karel understands how to move a block, but not a mile.
• Conversion from miles to blocks is straightforward, but results in a very long and unreadable program. How many Move(); instructions would be needed to move Karel one mile?
Karel can “learn” new commands via our instructions
• Our programs can furnish him with a dictionary of useful instruction names and their definitions.
• Each definition must be built from simpler instructions that Karel already understands.
• Given this ability to extend Karel’s vocabulary, we can solve our problems using instructions natural to our way of thinking.
How do we instruct Karel to learn a new command?
• For example, suppose we want Karel to learn the new command TurnRight, then how do tell Karel to remember this information?
• We know that if we instruct Karel to perform three TurnLeft instructions, then we can accomplish a TurnRight.
Defining a new function
void identifier(){
statement(s) that define the function
}
1 2 3 4 5 6
1
2
3
4
5
6
Suppose we want Karel to pickup the beeper shown in the situation file. Then what are Karel’s instructions?
#include <karel.h>int main(){ TurnOn(); TurnLeft(); TurnLeft(); TurnLeft(); Move(); PickBeeper(); TurnOff();}
1 2 3 4 5 6
1
2
3
4
5
6
The instructions for Karel to pickup the beeper shown in the situation file.
#include <karel.h>int main(){ TurnOn(); TurnLeft(); TurnLeft(); TurnLeft(); Move(); PickBeeper(); TurnOff();}
1 2 3 4 5 6
1
2
3
4
5
6
Now, lets modify Karel’s instructions so as to learn a new command, TurnRight.
#include <karel.h>void TurnRight(){ TurnLeft(); TurnLeft(); TurnLeft();}int main(){ TurnOn(); TurnRight(); Move(); PickBeeper(); TurnOff();}
1 2 3 4 5 6
1
2
3
4
5
6
Now, lets modify Karel’s instructions so as to learn a new command, TurnRight.
Defining a new function/command
void identifier(){
statement(s) that define the function
}
• Using this function definition mechanism to create a kind of dictionary for Karel, new commands can be defined to extend Karel’s vocabulary by writing functions.
• For example, although Karel does not have a predefined TurnRight(); command, we can user-define this instruction by writing the function shown in the previous slide and on the next slide.
void TurnRight(){ TurnLeft(); TurnLeft(); TurnLeft();}
• Similarly, we can define MoveAMile(); to mean eight Move(); instructions.
void MoveAMile(){ Move(); Move(); Move(); Move(); Move(); Move(); Move(); Move();}
Benefits of extending Karel’s vocabulary
• We can use instructions “natural” to us.
• As in the move a mile problem, the size of our programs can be significantly reduced.
• The smaller program is much easier to read and understand.
#include <karel.h>
int main()
{
TurnOn();
Move();
PickBeeper();
Move();
TurnLeft();
Move();
Move();
PutBeeper();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
TurnOff();
}
#include <karel.h>
int main()
{
TurnOn();
Move();
PickBeeper();
Move();
TurnLeft();
Move();
Move();
PutBeeper();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
Move();
TurnLeft();
TurnLeft();
TurnLeft();
TurnOff();
}
TurnRight();
TurnRight();
TurnRight();
#include <karel.h>void TurnRight(){
TurnLeft();TurnLeft();TurnLeft();
}
int main()
{
TurnOn();
Move();
PickBeeper();
Move();
TurnLeft();
Move();
Move();
PutBeeper();
Move();
TurnRight();
Move();
TurnRight();
Move();
TurnRight();
TurnOff();
}
A Stair Cleaning Task
• Karel is supposed to climb stairs and pick up the beepers on each step. When he is done, he should be standing on the top step, facing East.
#include <karel.h>…. int main() { TurnOn(); TurnLeft(); Move(); TurnRight(); Move(); PickBeeper(); TurnLeft(); Move(); TurnRight();…
If we invent a new instruction called ClimbAStep();
then the program can be written as:
#include <karel.h> int main() { TurnOn(); ClimbAStep(); PickBeeper(); ClimbAStep(); PickBeeper(); ClimbAStep(); PickBeeper(); TurnOff(); }
Now we must write the function ClimbAStep();
void ClimbAStep(){ TurnLeft(); Move(); TurnRight(); Move();}
Notice that this function depends on TurnRight(); , an instruction that we wrote previously.
Recapping how to dofunction definitions
• The reserved word void starts a new function followed by the name of the function.
• The name of the function specifies what the procedure is intended to do.
• The statements that perform the task are enclosed in matching curly braces { } . These statements specify how the new instruction does what the name implies.
• The two must match exactly – otherwise one or both need to be changed.
• To demonstrate this, there are no restrictions prohibiting the following instructions definition:
void TurnRight() { TurnLeft(); TurnLeft(); }
• This is a perfectly legal definition, but it is wrong because it does not accomplish what it should.