Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 4 / APRIL 1983 / PAGE 154

Karel the robot. (educational computer program) (evaluation) John J. Hirschfelder.

Karel The Robot

Robots are exciting all right. They do a terrific job on the assembly line, and someday they may clean the house, walk the dog, and keep us company. But what can they do for us today?

One thing they can do and are, in fact, doing is teaching programming concepts to children and adults. One such cybernetic teacher is Karel the Robot.

Karel is an instructional aide for people who want to learn Pascal on the Apple II. That is a welcome concept, indeed, for the multitudes whose first programming language is Basic, because learning Pascal not only calls for mastering many new ideas, but also for unlearning a variety of practices which are essential in Basic but bad habits in Pascal.

The author of this package, Richard E. Pattis, teaches programming at Stanford University. He invented Karel for his students and wrote a short (106 page) book, Karel the Robot: A Gentle Introduction to the Art of Programming, published by John Wiley and Sons. In the preface, Pattis observes, "The first few weeks of a programming course are crucial to the students' perception of the subject; it is during this period that they briefly glimpse the aesthetics of the discipline and are most receptive to new ideas. By starting with Karel the Robot, it will be easy for students to absorb a large number of useful, important, and sophisticated concepts quickly.'

The book is used as a text at Stanford and at the University of California, Berkeley, for the first one or two weeks of introductory programming classes.

Complex Simplicity

Karel is a very simple automaton. He lives on your monitor screen in his world, which is laid out with north-south avenues and east-west streets. Karel himself is always at an intersection. When he moves, one "step' is a full city block long, and he can face north, south, east, or west. On your screen, he is represented by , V, >, or <, depending on which way he is facing.

Karel's world is not empty. Some streets are blocked by walls, and on some corners there are markers called "beepers.' (Sorry, they don't beep.) Karel carries a bag of beepers, and he can pick them up and put them down. He is controlled by a program which you write. The following is a complete list of the actions Karel can perform.

turnleft

move (forward one block)

pickbeeper (from the ground to his bag

putbeeper (from his bag to the ground)

turnoff

Karel can also survey his world and make decisions based on his environment. He can tell if there is a beeper on the corner where he is standing; whether there are any beepers in his bag; and whether his front, right, or left is blocked by a wall.

That's all! By combining these statements with control structures in a Karel program, you can make the robot wander about his universe performing some very complex actions.

But what does this have to do with Pascal? Pattis has designed his robot language so that each of its syntactic features and control structures closely resembles a corresponding feature of Pascal, so that writing Karel programs is training for graduation to Pascal.

A Basic program begins with some array definitions which are followed by a long, unbroken stream of numbered executable statements. Sprinkled through the program there may be some subroutines, called by GOSUB statements. The code making up a subroutine may be all in one place, but it need not be. It ends with RETURN but may begin with anything--in fact, it can be entered at any point at all. The subroutine has no variables of its own, nor any parameters as a function has. Aside from GOSUB, the only control statements of significance are GOTO, IF . . . THEN, and FOR loops.

Differences In Pascal

A Pascal program looks entirely different. It begins with definitions for named constants, and follows with declarations of all the program variables--not just the arrays. Then come the subroutines, called procedures, each of which has a precisely defined beginning, a single end, its own variables, and possibly some parameters. At thy very end comes the body of the program, which is usually quite short and consists mostly of procedure calls. There is a GOTO statement in Pascal, but it is rarely used.

Thus writing a Pascal program requires a different kind of organizational thinking than writing one in Basic. It is the Pascal type of program organization --called top-down or structured programming--that the Karel language teaches. So let's look at some of the concepts of the Pascal language, and see how features of the Karel language teach them.

Pascal has extensive data structuring, including records as well as arrays. Karel has none (except for Karel's world, which is not variable); its purpose is to teach program organization, not data organization.

Pascal has compound statements, delimited by BEGIN and END. For example

BEGIN X:=X+1; Y:=Y+1 END.

A Pascal compound statement is not the same as a Basic multi-statement line, which is just a space-saving convenience. Compound statements work nicely with IF statements, which in Pascal can have ELSE clauses. Karel has these too, and they can be used to define a complex action. Although Karel can only turn left, we can write a compound statement to make him turn right:

BEGIN turnleft; turnleft; turnleft END.

The more complex statement in Figure 1 tells Karel to go one block forward, unless the way is blocked, in which case he is to move two blocks backward.

Pascal has FOR loops like Basic, but it also has the WHILE . . . DO statement which causes some action to be performed as long as some condition remains true. Karel has this feature in exactly the same form. The following statement instructs Karel to move forward until he comes to a wall:

WHILE front-is-clear DO move.

Finally, Pascal has the procedure. If all data are ignored, a procedure is just a compound statement given a name, so that is can be cited elsewhere in the program. Karel's analog is the new instruction definition. Figure 2 shows two examples.

A Karel program can be built from these constructs. Figure 3 is Karel following a wall to his right, looking for a beeper.

And there you see the fundamentals of the structured programming style: procedures, blocks, if-then-else, while-do. A typical Karel program, like a well-written Pascal program, is 95% new instruction definitions and 5% body.

Summary

The Karel software package consists of two disks, labeled KAREL: and KAREL2: Each disk boots the UCSD Pascal operating system. A user's manual, which contains clear and complete instructions for configuring a one or two drive system, for using the Karel simulator program and for running the demonstrations, is included.

The package does not contain a copy of the Pascal text editor. You must provide this yourself. Nor does the manual tell you how to use the text editor, or anything about Karel's language--for this you must read Pattis's book.

The simulator is comprehensive and easy to use. Besides putting Karel through his paces as defined by your program, it offers a choice of speeds, single-stepping, and other debugging aids. The simulator includes a "world-builder' for defining Karel's environment and initial conditions. With the world-builder, you can build and destroy walls, move Karel around, and strew beepers on street corners. You can't pick up a beeper, so if you make a mistake and put one where you don't want it, you have to start over.

The Karel package is aimed at the high school or college classroom in which Apples and Pascal are available. For about the first two weeks of a Pascal class, Pattis's book can be used as a text, with assignments from the numerous exercises in the book to be run on the computer. Then the class can move quickly on to data structures and Pascal. Karel can be fun, and there is some danger of getting distracted from the real objective and spending too much time with Karel.

I recommend a serious look at Karel to any Pascal instructor. If, however, you are an individual programmer, have just gotten Pascal for your Apple, and are trying to learn the language, I suggest that you get any of the Pascal tutorials and jump right in.

The two-disk Karel package with manual costs $85 and is available in a standard 40-column version and in a version for users of 80-column cards. Also available, for $150, are two disks containing solutions to all the problems in the Karel the Robot book. Versions of the simulator for the IBM Personal Computer and the Terak are forthcoming.

Figure 1.

IF front-is-clear THEN move ELSE BEGIN turnleft; turnleft; move; move; turnleft; turnleft END.

Figure 2.

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

Figure 3.

BEGINNING-OF-PROGRAM DEFINE-NEW-INSTRUCTION try-again AS IF right-is-clear THEN BEGIN turnright; move END ELSE IF front-is-clear THEN move ELSE IF left-is-clear THEN BEGIN turnleft; move END ELSE BEGIN turnright; turnright; move END; BEGINNING-OF-EXECUTION WHILE not-next-to-a-beeper DO try-again; turnoff END-OF-EXECUTION END-OF-PROGRAM.

Products: Cybertronics International Karel the Robot (computer program)