Karel
the robot is an educational tool for teaching programming. Karel is a robot
with a simple vocabulary of actions it can perform and conditions it can sense.
Karel lives in world of streets and intersections. Karel's world contains
walls which are impassable, and beepers. Beepers are little
devices which emit a quiet beeping sound. Karel can tell when a beeper is
present and can pick up a beeper, placing it into a carry bag, and drop a beeper
out of his carry
bag.
Karel the robot is
described in the book Karel
The Robot: A Gentle Introduction to the Art of Programming (second
edition, 1994) by Richard E.
Pattis (with contributions from Jim Roberts and Mark Stehlik). The first
edition of the book was published in 1981.
Using Karel to teach programming has the following advantages:
There are a number of
simulators for Karel. I've created an implementation for the Palm Pilot using PocketC. You write a PocketC program using a
subset of the language (including Karel's primitives which I've implemented).
This is compiled with PocketC and run. A major advantage of having a Palm Pilot
implementation is that you can practice programming on the run (on public
transport, during boring lectures :-), etc.) without needing to lug around a
laptop or worry about batteries running out.
IMPORTANT: You will need a registered version of PocketC since you'll be compiling programs. PocketC isn't expensive and if you're learning programming it's useful to have.
Installing Karel: I assume you already have PocketC installed. You'll need to download this memo (also available as a zip file) and sync it onto your palmpilot (open it with a text editor such as notepad, select all, copy, paste into a new memo in the Palm Desktop, then sync).
Karel can perform the following actions:
Karel also can sense various conditions:
Additionally, the implementation provides a number of useful operations:
The following simple program can be used to create worlds:
// KarelMakeWorld
include "karel"
main() {
turnOn();
editWorld();
saveWorld(gets("Name?"));
}
Run this program, edit the world and write a letter (any letter) to
finish. The program will ask for a memo name to save the world to. When writing
your Karel program, start off with a loadWorld. For example,
suppose you've edited a world and saved it to a memo titled
myWorld. Then a Karel program which operated in that world
would look like this: // KarelExample
include "karel"
main() {
turnOn();
loadWorld("myWorld");
... your code here ...
}
This illustrates the general form of a Karel program:
In addition to the actions and sensors described above, Karel programs can make use of standard PocketC constructs. Specifically, the following are useful:
movePickup() { move(); if (beeper()) pickup(); }
Let us consider now a simple example: we want Karel to move forward collecting all beepers found until he hits a wall. Then Karel should move north (i.e. up) until he finds a space to his left, then drop the beepers in a row, and park. This is done by the following program.
// KarelExample
include "karel"
main() {
turnOn(); setPause(370);
loadWorld("karel1world");
while (!blocked()) {
move();
if (beeper()) pickup();
}
turnLeft(); move();
while (wallLeft()) move();
turnLeft(); move();
while (holdingBeeper()) {
drop(); move();
}
turnRight();
move(); move();
waitp();
}
The waitp(); tells PocketC to wait for a pen tap before
finishing the program.
To compile the program tap on the PocketC icon, tap on the compile button, select the program (KarelExample), then tap on the compile button. To run the program run PocketC, select the program rom the list of compiled programs, and tap on the execute button.
The following animated gif shows the program running (click it for a larger version):