Introduction

Although robot Karel is already known for a long time, we have found no standard how the syntax of this language should look like anywhere. Each implementation of Karel we have seen had a different syntax (especially on 8-bit computers). That is why we have created our own syntax. The principle of programming of the robot is simple. The robot knows the elementary commands, and some control commands (conditions , cycles). It is possible to create another commands using a procedure. It is a sequence of already known commands. A new command, once is defined, can be used as a known command. Although it is possible to use any text editor to create these procedures, you can avoid many troubles by using program Karel for this purpose. You will find all examples which are included in this help also in the file help.krl, which is distributed together with Karel. We hope, that the syntax will be easy to understand for you.

Keywords

Keywords in the language Karel are elementary commands, conditions, or cycles. "Procedure", and "Else" are another keywords. The language Karel is not case sensitive. "MOVE" is therefore the same as "Move" or "move" (or even "mOVE").

Elementary commands

Elementary commands are commands, which robot Karel understands and perfom some activity as a reaction. They are "indivisible". They aren't composed of no commands and on the contrary all other user defined procedures are composed of them. Elementary commands are:

Move

Karel will make a step. He will therefore move by one field in the direction he faces and he won't change it. If Karel is standing against the wall, error occurs.

Put

Karel will put a brick on the field, on which he is standing. Neither he will move to another field nor turn to another direction. It is possible to set the maximum number of bricks on one field. If the number of bricks on the field on which Karel was standeing before performance of the command Put exceeds the maximum limit, error occurs.

Take

Karel will take (remove) a brick of the field he is standing on. Neither he will move to another field nor turn to another direction. If there is no brick on the field Karel was standing on before performance of the command Take (the field is empty), error occurs.

Turnleft

Karel will turn to the left. He will stay on the field he was standing before performance of the command and neither will take nor put any brick.

Conditions (branching)

Conditions serve for better control of the robot. By means of a condition, it is possible to distinguish a sequence of commands, which are to be performed if the condition is true from the commads which are to be performed if the condition isn't true. Syntax of the condition is:

Condition
{
   #A sequence of the commands, which are to be performed if that the condition
   #is true. You needn't write any command but there have to be brackets here.
}
Else
{
   #A sequence of commands which are to be performed in case of untrue
   #condition. You need't write any command, but there have to be brackets
   #here. If only empty brackets should follow the word ELSE, it's better
   #to ommit the word ELSE. See the other possibillity.
}

The other possibillity:

Condition
{
   #A sequence of commands which are to be performed in case of true condition.
   #You need't write any command, but there have to be brackets here.
}

Conditions

IsNorth

Condition is true if Karel faces the north (up)

IsWall

Condition is true if Karel is standing against the wall. He is therefore in such a position and is facing in such a direction that if he made a Move, he will collide with the wall.

IsBrick

Condition is true if Karel is standing on the field where a brick lies.

Examples

  1. I want Karel to make a move only if he faces the north.
    IsNorth
    {
        Move
    }
  2. I want Karel to make a move only if he isn't standing against the wall.
    IsWall
    { }
    Else
    {
        Move
    }
  3. If there is any brick in the field, I want Karel to take it, else I want him to turn to the left and make a move.
    IsBrick
    {
        Take
    }
    Else
    {
        Turnleft
        Move
    }

Cycles

Both Cycles and conditions serve for better control of the robot. By means of a condition, it is possible to repeat a certain sequence of commands, while a certain condition is (or is not) met. The syntax of a cycle:

While condition
{
   #A sequence of commands that should be repeatedly executed, while the condition is met.
}

Or the second possibillity:

While NOT condition
{
   #A sequence of commands that should be repeatedly executed, while the condition is not met.
}

There are three kinds of conditions. Their syntax has been already explained in the chapter on branching.

Examples of cycles

  1. I want Karel to turn to the north unless I knew which direction he faces.
    While NOT IsNorth
    {
        Turnleft
    }
  2. I want Karel to take a row of bricks in the direction he faces. until he encounters a field with no brick.
    While IsBrick
    {
        Take
        Move
    }

Comments

Comments destined only to the author can be inserted into the program for Karel. Karel ignores these comments, they are used just for making the program more transparent. A comment starts with the character "#", and continues until the end of the line..

Example of the commented piece of program:

While NOT IsWall   #I will reach the wall.
{
      Move  # This is the move.
      # Neve will occur:   Put
}

Procedures

Procedures is a new command Karel will learn. In order to perform a procedure Karel have to know all the commands it consists of. Keyword Procedure is used to define a new procedure. It is followed by a character string indicating a name of the new command.

A name of the command must meet these requirements:

  • Begins by a character or by a number
  • Contents characters or numbers
  • A name is not equal to any other keyword

For example, names enabled are: "Move3", "and", "TurnRight"

For example, names disabled are: "%er", "Move", "Double Move", etc...

A body in itself (sequence of commands which are to be performed) is in the brackets.

Example of a procedure

Procedure experiment1
{
   #I will collide with the wall on purpose
   While NOT IsWall
   {
        move
   }
   move# BANG!!!!!!!!!
}

Warning

Writting of comments out of the procedure (out of the brackets "{" , "}") is not enabled.

Recursive call

Recursive call is calling a procedure (a command) up from "itself". Recursion can be direct or indirect. The language Karel supports using both direct and indirect recursion.

Example of the direct recursion

Procedure ToNorth
{
   IsNorth
   {}
   Else
   {
      TurnLeft
      ToNorth # Procedure calls itself
   }
}

Consider that this procedure will turn Karel to the north no matter what direction he faces. We have outlined another way how to turn Karel to the north in the chapter on cycles.

The other type of recursion is the indirect recursion. A procedure calls again itself but it will use one or more procedures to do that.

Example of the indirect recursion

Procedure r1
{
   move
   r2
}

Procedure r2
{
   IsWall
   { }
   Else
   {
      r1
   }
}

Will you find out by yourself what will Karel do if you order Karel to perform a command r1?

Frequent mistakes

In this chapter, we will point out frequent mistakes, which you may make writting programmes for robot Karel. Here is some advice how to avoid these mistakes.

  • Don't write comments out of the body (outer brackets) of the procedure..
  • A number of brackets "{" in a procedure must be the same as a number of brackets "}".
  • Don't use keywords as a name of a new procedure.
  • Is the best to create a new procedures with the program Karel, not with any other program (or text editor).
  • Don't forget that all that you write behind a symbol "#" till the end of the line is ignored by Karel.

Explication of some of the error statements

Not waiting end of source text in procedure XXX
The termination of the body in the procedure occured although the syntax don't allow it. Check on, e. g. if the brackets in conditions, and cycles are correct.
Can't find begin of block (char "{") in XXX
Check out if a symbol "{" is indeed everywhere it is supposed to be. In cycles, conditions, behind the word "ELSE".
Can't find the end of the block (char "}") in XXX
Check out if a symbol "}" is indeed everywhere it is supposed to be. In cycles, conditions, behind the word "ELSE".
Keyword NOT is used wrong in procedure XXX
Check out if each "NOT" is preceded by "WHILE" and followed by a condition
Keyword ELSE is used wrong in procedure XXX
Check out if each "ELSE" in the procedur XXX is preceded by condition, and each "NOT" is followed by a bracket "{".
Behind keyword WHILE (resp. WHILE NOT) must be condition in the procedure XXX
Check out if each WHILE (resp. WHILE NOT) is followed by a condition.
Keyword can't be used as a name of the procedure XXX
Change a name of the procedure. A keyword XXX musn't be a name of procedure.

Errors which occur during a performance of a procedure:

I'm sorry but I can't put here more brick.
Karel put a brick on the field which there is already one on.
I can't take brick.
You are trying to take a brick from the field which is empty.
Ooops, I was collide with a wall!
Karel has collided with the wall. He therefore made a "Move" when he was standing against the wall and was facing it..
Procedure XXX has no body.
You use a command named XXX but Karel doesn't know it.

Joke of day: Do you know, what is common for the language Smalltalk and the language Karel? Both of them use the same character for comments.