(i) move,
turnleft, putbeeper, pickbeeper,
and putbeeper aredef instructions
NOTES: 1. "aredef"
and "isdef" mean: "are
(or: is) by
definition"
2. I will be defining the term instruction
in pieces; this first piece shows you
the 5 basic (or "primitive") instructions;
such a definition is also sometimes
called an "ostensive" definition.
(ii) any sequence of instructions, separated
by semicolons (";") and surrounded by
the reserved words "begin" and "end"
isdef an instruction.
NOTE: Here, the first occurrence of "instruction"
is being used to define the second occurrence.
But that's OK: the first time you read this,
it just means that any sequence of "move",
"turnleft", etc., separated by ";" and surrounded
by "begin/end" is an instruction. Then, once
we have these, we can also say that any
sequence consisting of any "begin...end"
instruction or any of the others, with ";"
and "begin/end" in appropriate places, is
also an instruction, etc. In other words,
we are building up the notion of an "instruction"
piece by piece.
NOTE: I could also have given this clause of the
definition as follows:
begin
<instruction> ;
.
.
isdef an instruction
.
<instruction>
end
I will use this method in clause (iii):
(iii) if <test>
then <instruction>
isdef an instruction.
So, an instruction could be as simple as one of the 5
basic ones, or as complicated as something like this:
if facing-east
then
begin
move;
turnleft;
begin
turnleft;
turnleft;
turnleft;
end;
if facing-east
then turnoff
end
Note that this is a single if-then instruction
(these are sometimes also called "conditional"
or "choice" or "selection" instructions).
Its "then" part consists of a single instruction,
which is a begin-end sequence.
That begin-end sequence consists of 4 instructions:
1 = move
2 = turnleft
3 = a begin-end sequence
(consisting
of 3 instructions)
4 = a conditional instruction,
whose "then" part is a single "turnoff"
instruction
In clause (iii) of our definition of "instruction"
(which is not yet complete; it will be extended
in
the next lecture),
"if" and "then" are reserved words that must appear
exactly as shown,
"<test>" and "<instruction>" stand for expressions
that you, the programmer, must supply.
c) In particular:
"<test>" is a
statement that can be true or false.
d) Here's what the conditional instruction means:
* When Karel hears a conditional,
he decides whether <test> is true or false
* If <test> is true,
then Karel does <instruction>
* If <test> is false,
then Karel ignores <instruction>
& listens to the next one.
(I won't try to draw the flowchart here; see Lecture
Notes
#4 for a sample)
e) For a list of all and only the allowable <test>s,
see the Karel
Programming Summary.
NOTE: In Karel's language,
you cannot define new tests
(but you can in Pascal and all other high-level
programming languages)
e.g., Karel is at (1,1), facing in any direction.
He must travel on a diagonal,
harvesting beepers
There are many initial situations; here are 2 samples:
Karel is facing east; there
are beepers at (2,2), (4,4), (5,5), etc.
Karel is facing west; there
are beepers at (4,4), etc.
(note that I am being vague
here; I'm only concerned about
sample situations)
Rough plan (= first approximation to final algorithm) (= top of TDD):
1. go to diagonally-opposite corner
2. harvest a beeper
3. go to diagonally-opposite corner
4. harvest a beeper
etc.
Refinement of step 1:
1.1 move
1.2 turnleft
1.3 move
But if you try this in the first sample above, it will fail after Karel
gets to the second corner,
And if you try it in the second sample above, it will fail immediately.
So, we see that we need to refine the initial problem specification
to make sure that Karel is facing east to begin with, which is the
only way that 1.1-1.3 will work to get Karel to the diagonally-
opposite corner.
So, add:
1.0 face-east
But now we have to refine 1.0:
1.0.1 if not-facing-east
then turnleft
1.0.2 if not-facing-east
then turnleft
1.0.3 if not-facing-east
then turnleft
You should check for yourself that this will cause Karel
to face east, no matter which direction he is facing in
initially.
It is important that you understand that this works,
since the point of this lecture is to understand the
if-then instruction.
HINT: If Karel is facing east,
he doesn't have to do anything;
make sure that the sequence of instructions 1.0.1-1.0.3
has Karel do nothing if he is already facing east
If Karel is facing south, he only has to turn left once.
If Karel is facing west, he only has to turn left twice.
If Karel is facing north, he only has to turn right once
(which = turning left 3 times!)
Test out each of these situations!
There is nothing left to refine in step 1,
so we can turn our attention to step 2:
The refinement of step 2 is:
2.1 if next-to-a-beeper
then pickbeeper
There is nothing left to refine in step 2,
since 2.1 is an instruction that Karel can execute
directly.
Only one problem remains:
How do we get Karel to stop?
For now, we just tell him how many diagonal corners
to visit.
Let's say that we tell him to visit 8 corners.
Final program:
beginning-of-program
TRY THIS OUT!
define-new-instruction face-east
as
begin
if not-facing-east
then turnleft;
if not-facing-east
then turnleft;
if not-facing-east
then turnleft;
define-new-instruction
go-to-diagonally-opposite-corner as
begin
face-east;
move;
turnleft;
move
end;
define-new-instruction harvest-beeper
as
begin
if next-to-a-beeper
then pickbeeper
end;
define-new-instruction get-beeper
as
begin
go-to-diagonally-opposite-corner;
harvest-beeper
end;
beginning-of-execution
get-beeper;
get-beeper;
get-beeper;
get-beeper;
get-beeper;
get-beeper;
get-beeper;
get-beeper;
turnoff
end-of-execution
end-of-program
Copyright © 2000 by
William
J. Rapaport (rapaport@cse.buffalo.edu)
file: 111F00/lecturenotes8.22sp00.html