CSE 111, Fall 2004

KAREL PROGRAMMING SUMMARY

The following summary of Karel the Robot's programming language is adapted from:

Pattis, Richard E.; Roberts, Jim; & Stehlik, Mark (1995), Karel the Robot: A Gentle Introduction to the Art of Programming, Second Edition (New York: John Wiley & Sons).


1. PRIMITIVE (OR: "BASIC") INSTRUCTIONS

SYNTAX
(what the instruction
looks like in
Karel's lang
SEMANTICS
(what the instruction means
(i.e., what Karel does)
1. move IF Karel sees no walls,
    THEN Karel moves forward
             1 block in direction
             he's facing
     ELSE IF Karel sees a wall,
               THEN "error shutoff"
2. turnleft Karel pivots 90° left
3. pickbeeper IF there is at least one beeper
    on Karel's corner,
    THEN Karel picks up one
              beeper & puts it in
              his bag
    ELSE error shutoff
4. putbeeper IF there is at least one beeper
    in Karel's bag,
    THEN Karel puts it on his
              corner
     ELSE error shutoff
5. turnoff Karel turns himself off

2. BLOCK-STRUCTURING (OR "SEQUENCING")

6.    begin
         <instruction>
               .
               .
               .
         <instruction>
      end

3. CONDITIONAL (OR "CHOICE", OR "SELECTION")
    INSTRUCTIONS

7.    if <test>
          then <instruction>

8.    if <test>
          then <instruction>
          else <instruction>

4. REPETITION (OR "LOOPING") INSTRUCTIONS

9.      iterate <positive-number> times
            <instruction>

10.    while <test> do
              <instruction>

5. MECHANISM FOR DEFINING (OR "NAMING")
    NEW INSTRUCTIONS

define-new-instruction <new-name> as
    <instruction>

6. SPECIFYING A COMPLETE PROGRAM

beginning-of-program
   define-new-instruction <new-name> as
        <instruction>;
            .
            .
            .
   define-new-instruction <new-name> as
        <instruction>;
    beginning-of-execution
        <instruction>;
            .
            .
            .
        <instruction>
  end-of-execution
end-of-program

7. ANGLE-BRACKETED WORDS

<instruction> Any of Karel's instructions 1 - 10
<new-name> Any new non-reserved word,
using lower-case letters, numbers, &/or "-")
<positive-number> Any positive number
<test> Any of the following:
front-is-clear, front-is-blocked

left-is-clear, left-is-blocked

right-is-clear, right-is-blocked

next-to-a-beeper, not-next-to-a-beeper

facing-north, not-facing-north

facing-south, not-facing-south

facing-east, not-facing-east

facing-west, not-facing-west

any-beepers-in-beeper-bag, no-beepers-in-beeper-bag


Copyright © 2004 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F04/karel-prog-sum-2004-09-26.html