To keep manufacturing costs down, the factory only built gears in Guido to move forward and to turn left. I read in the instruction manual that Guido has the ability to learn to do other things. For example, if Guido turns left three times, he will be facing right. But you as the robot programmer need to tell Guido how to do this.
We do this by defining a new instruction turnright
as a series of
other instructions, specifically three turnleft
instructions.
The definition looks like this:
define turnright: turnleft turnleft turnleft
This is an example of a compound statement, which means it is made
up of two parts. The first part consists of define
followed
by the name of the instruction you are defining, followed by a colon
(:
). The second part consists of one or more instructions
indented the same number of spaces. See if you can figure out what this
complete program does.
define turnright: turnleft turnleft turnleft move turnright move turnright move turnright move turnright turnoff
The three turnleft instructions make up what is called a block of code, several instructions acting together as one. All GvR programs end with a turnoff instruction.
You should be able to "hand trace" the operation of this program to discover that Guido will walk in a small square, returning to his starting position.
Once you have defined a new instruction, you can use that instruction as if it
were built-in to GvR. Define an instruction backup
that makes
Guido back up one block, leaving him facing in the same direction. Then use
backup
in a complete program that has Guido start at the corner
of Second Street and Third avenue, move
three blocks north,
backup
one block, turnright
, and then
move
two blocks east.
Copyright © 2003 Roger Frank.