Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been...

24
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps

Transcript of Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been...

Page 1: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

Extending Karel’s Vocabulary

This PPT originated with Dr. Judy HankinsModifications have been done by Dr. Untch

& Dr. Cripps

Page 2: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications 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.

Page 3: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

• 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?

Page 4: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 5: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 6: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

Defining a new function

void identifier(){

statement(s) that define the function

}

Page 7: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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?

Page 8: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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.

Page 9: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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.

Page 10: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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.

Page 11: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

Defining a new function/command

void identifier(){

statement(s) that define the function

}

Page 12: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

• 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.

Page 13: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

void TurnRight(){ TurnLeft(); TurnLeft(); TurnLeft();}

Page 14: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

• Similarly, we can define MoveAMile(); to mean eight Move(); instructions.

void MoveAMile(){ Move(); Move(); Move(); Move(); Move(); Move(); Move(); Move();}

Page 15: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 16: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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();

}

Page 17: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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();

Page 18: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#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();

}

Page 19: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 20: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

#include <karel.h>…. int main() { TurnOn(); TurnLeft(); Move(); TurnRight(); Move(); PickBeeper(); TurnLeft(); Move(); TurnRight();…

Page 21: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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(); }

Page 22: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 23: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

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.

Page 24: Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

• 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.