Go to the first, previous, next, last section, table of contents.


Turning Right

The Karel language has a turnleft primitive, but lacks a turnright primitive. One of the first instructions a new Karel programmer does is define the turnright instruction for Karel. As the old adage goes, "two wrongs don't make a right, but three lefts do".

The following example demonstrates how to define a new instruction for Karel out of the existing primitives. This example will cause Karel to walk in a square to the right, instead of the left as in the previous example.

{ A simple karel program to walk in a square to the right }

BEGINNING-OF-PROGRAM

  DEFINE-NEW-INSTRUCTION turnright AS
    ITERATE 3 TIMES
      turnleft;

  BEGINNING-OF-EXECUTION
    move;
    turnright;
    move;
    turnright;
    move;
    turnright;
    move;
    turnright;
    turnoff
  END-OF-EXECUTION

END-OF-PROGRAM


Go to the first, previous, next, last section, table of contents.