The Art of Robot Programming

The First Robot Program
Programming Errors
Extending Karel's Knowledge
The Second Robot Program
Programming Style

The first Robot Program

BEGINNING-OF-PROGRAM
   BEGINNING-OF-EXECUTION   
      move;
      turnleft;
      move;
      turnleft;
      move;
      turnleft;
      move;
      turnleft;
      turnoff
   END-OF-EXECUTION
END-OF-PROGRAM
Karel pays strict attentention to grammar and punctuation rules. Karel understands three types of symbols:
puntuation marks (semicolon)
instructions (move etc.)
reserved words

To distinguish between reserved words and instructions, instructions will be written in lower-case, reserved words in upper-case. Reserved words are: BEGINNING-OF-PROGRAM, BEGINNING-OF-EXECUTION, END-OF-EXECUTION, END-OF-PROGRAM, BEGIN, END, DEFINE-NEW-INSTRUCTION, AS

Note: The instruction turnoff should always appear exactly once in a program, just before the END-OF-EXECUTION statement. The indentation used in this program is nice for us humans, but for Karel the program looks the same as

BEGINNING-OF-PROGRAM BEGINNING-OF-EXECUTION move; turnleft; move; turnleft; move; turnleft; move; turnleft; turnoff END-OF-EXECUTION END-OF-PROGRAM

Programming Errors

Start of task

End of task

The Program

BEGINNING-OF-PROGRAM
   BEGINNING-OF-EXECUTION  
      move;
      pickbeeber;
      turnleft;
      putbeeper
      move
   END-OF-EXECUTION
END-OF-PROGRAM
When you first write a robot program, chances are that it will contain some errors. There are four types of errors in robot programming:
Lexical Errors
Karel finds an instruction or keyword that he does not know
Syntactical Errors
The rules for robot programming are violated, for example a missing semicolon or a keyword at the wrong place
Execution Errors
Karel performs an error shutoff. This could happen because
he runs into a wall
a pickbeeper command on a beeper-less corner
a putbeeper command with an empty beeper bag
Intent Error
Karel sucessfully performs a task which is different from his assigned task

The program on the left contains all of these four errors. Can you find and identify them ?

Extending Karel's Knowledge

Originaly Karel's vocabulary is very limited. We would get cramps trying to have Karel move, say, 1 mile ( = 160 blocks) north; we would have to type 160 move's. And, at the very least, Karel should really know how to turn right. Therefore, we need the possibility to extend Karel's vocabulary.

To teach Karel new words, we can use the reserved word DEFINE-NEW-INSTRUCTION, as in the following example:

   DEFINE-NEW-INSTRUCTION turnright AS
      BEGIN
         turnleft;
         turnleft;
         turnleft
      END;

In other words, we have now taught Karel to turn right by executing left turns. Note that turnright for Karel means exactly what the instructions after the word AS say. The fact that turnright has somewhat of a meaning in the English language is of no concern to Karel. We could have defined:

   DEFINE-NEW-INSTRUCTION selfdestruct AS
      BEGIN
         turnleft;
         turnleft;
         turnleft
      END;

When Karel would read the instruction selfdestruct he would of course not commit suicide, but rather execute three left turns.

Puntuation: The instruction just before the END has no semicolon. There is a semicolon after the END.

The second Robot Program

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.

BEGINNING-OF_PROGRAM
   DEFINE-NEW-INSTRUCTION turnright AS
      BEGIN
	turnleft; 
        turnleft;
        turnleft
      END;
   DEFINE-NEW-INSTRUCTION climb-stair AS
      BEGIN
	turnleft;
        move;
        turnright;
        move
     END;
   BEGINNING-OF-EXECUTION
      climb-stair;
      pickbeeper;
      climp-stair;
      pickbeeper;
      climb-stair;
      pickbeeper;
      turnoff
   END-OF-EXECUTION
END-OF-PROGRAM

Good Programming Style

A program that works perfectly is not considered a good program; it is simply a working program. To write a good robot program you must follow these guidelines:

a program must be easy to read and understand
a program must be easy to debug
a program must be easy to modify to solve a variation of the original task
Example:

During the summer Karel works as a field laborer. His task is to harvest the field of beepers. He can relax and turnoff as soon as he has harvested all beepers.

Here are three sketched solutions to the problem. Two make use of defining new instructions which are supposed to do exactly what their English equivalent indicates. Which of those programs would be considered the best program ?

Program 1 Program 2 Program 3
BEGINNING-OF-EXECUTION
   move;
   pickbeeper; 
   move;
   pickbeeper;
   ...
   turnleft;
   move;
   turnleft;
   pickbeeper;
   move;
   ...
   turnleft;
   turnleft;
   turnleft;
   move;
   turnleft;
   turnleft;
   turnleft;
   pickbeeper;
   move;
   ...
END-OF-EXECUTION
BEGINNING-OF-EXECUTION
   move;
   harvest-one-row;
   go-to-next-right-row;
   harvest-one-row;
   go-to-next-left-row;
   harvest-one-row;
   go-to-next-right-row;
   harvest-one-row;
   go-to-next-left-row;
   harvest-one-row;
   got-to-next-right-row;
   harvest-one-row;
   turnoff
END-OF-EXECUTION
BEGINNING-OF-EXECUTION
   move;
   harvest-two-rows;
   turn-around;
   harvest-two-rows;
   turn-around;
   harvest-two-rows;
   turnoff
END-OF-EXECUTION

(bgw)