define-new-instruction
<new instr name> as
<instr>
where:
* "define-new-instruction"
and "as" are
reserved words
*
<new instr name> is the name you give
to your new instruction
* <instr> can be:
(a) any of the 5 basic instructions
OR (b)
a sequence of basic or
previously-defined instructions,
separated by semicolons (";"),]
and surrounded by the reserved
words "begin" and "end"
e.g., define-new-instruction
changelocation as
move
e.g., define-new-instruction
changedirection as
turnleft
e.g., define-new-instruction
write as
putbeeper
e.g., define-new-instruction
erase as
pickbeeper
e.g., define-new-instruction
halt as
turnoff
e.g., define-new-instruction
tl as
turnleft
e.g., define-new-instruction
turnright as
begin
turnleft;
turnleft;
turnleft
end
NOTE: In a real Karel program,
*
the define-new-instruction command
goes after the
"beginning-of-program" reserved word,
*
it must be separated from the next part
of the program (which will either be the
"beginning-of-execution" reserved word
or else another "define-new-instruction")
by a semicolon
*
you don't need a semicolon before the
reserved word "end", but the compiler
in our implementation will complain
if you leave it out :-)
e.g., the program from Lecture Notes #5 could
now be written more briefly as:
beginning-of-program
define-new-instruction
turnright as
begin
turnleft;
turnleft;
turnleft
end;
beginning-of-execution
move;
turnright;
move;
pickbeeper;
turnleft;
turnoff
end-of-execution
end-of-program
NOTE: Karel will execute this program exactly
as
he executed the earlier version
(in particular, he will continue to turnleft
3 times):
You cannot create new instructions using
"define-new-instruction";
you can only give a new name to old ones.
NOTE: From now on, when I describe worlds,
I will write (A, S) to represent Ath Ave.
and Sth St.
e.g., Initial Situation:
Karel at origin, facing N;
1 beeper each at:
(2,2), (3,3), (4,4)
Final Situation: Karel at (4,4),
facing N;
no beepers at any
corners
long solution:
(21 instructions, not counting reserved
words or "turnoff")
(note that each group of 7 instructions
is the same as for the previous program!)
beginning-of-program
beginning-of-execution
move;
turnleft;
turnleft;
turnleft;
move;
pickbeeper;
turnleft;
move;
turnleft;
turnleft;
turnleft;
move;
pickbeeper;
turnleft;
move;
turnleft;
turnleft;
turnleft;
move;
pickbeeper;
turnleft;
turnoff
end-of-execution
end-of-program
medium
solution:
(15 instructions)
beginning-of-program
define-new-instruction
turnright as
begin
turnleft;
turnleft;
turnleft
end;
beginning-of-execution
move;
turnright;
move;
pickbeeper;
turnleft;
move;
turnright;
move;
pickbeeper;
turnleft;
move;
turnright;
move;
pickbeeper;
turnleft;
turnoff
end-of-execution
end-of-program
short
solution:
(3 instructions!)
beginning-of-program
define-new-instruction
turnright as
begin
turnleft;
turnleft;
turnleft
end;
define-new-instruction
getbeeper as
begin
move;
turnright;
move;
pickbeeper;
turnleft
end;
beginning-of-execution
getbeeper;
getbeeper;
getbeeper;
turnoff
end-of-execution
end-of-program
NOTES:
* The definition of "getbeeper" must come after
the definition of "turnright",
since "getbeeper"
is defined in terms of "turnright"
* The short program takes Karel just as long to
execute as the long program
(maybe a bit
longer, since he has to
look up the definitions
of the new instructions
each time he hears one)
* But the short program is easier for us humans to
type, to read, and to understand
* It also shows the structure of the program more
clearly than the longer
programs.