Gödel, Escher, Bach An Eternal Golden Braid by Douglas R. Hofstadter

(Dana P.) #1
learned motions, etc.-and the nestedness, or chunkedness, can go back
many layers until you hit primordial muscle-actions. And thus, the reper-
toire of BlooP programs, like the repertoire of a skater's tricks, grows, quite
literally, by loops and bounds.

BlooP Tests

The other feature of BlooP is that certain procedures can have YES or NO
as their output, instead of an integer value. Such procedures are tests,
rather thanfunctions. To indicate the difference, the name of a test must
terminate in a question mark. Also, in a test, the default option for OUTPUT
is not 0, of course, but NO.
Let us see an example of these last two features of BlooP in an
algorithm which tests its argument for primality:

DEFINE PROCEDURE "PRIME?" [N]:
BLOCK 0: BEGIN
IF N = 0, THEN:
QUIT BLOCK 0;
CELL(O) ¢: 2;
LOOP AT MOST MINUS [N,2] TIMES:
BLOCK t: BEGIN
IF REMAINDER [N,CELL(O)] = 0, THEN:
QUIT BLOCK 0;
CELL( 0) ¢: CELL( 0) + t;
BLOCK t: END;
OUTPUT ¢: YES;
BLOCK 0: END.

Notice that I have called two procedures inside this algorithm: MINUS and
REMAINDER. (The latter is presumed to have been previously defined, and
you may work out its definition yourself.) Now this test for primality works
by trying out potential factors of N one by one, starting at 2 and increasing
to a maximum of N -t. In case any of them divides N exactly (i.e., gives
remainder 0), then we jump down to the bottom, and since OUTPUT still
has its default value at this stage, the answer is NO. Only if N has no exact
divisors will it survive the entirety of LOOP t; then we will emerge smoothly
at the statement OUTPUT ¢: YES, which will get executed, and then the
procedure is over.

BlooP Programs Contain Chains of Procedures


We have seen how to define procedures in BlooP; however, a procedure
definition is only a part of a program. A program consists of a chain of
procedure definitions (each only calling previously defined procedures), op-
tionally followed by one or more calls on the procedures defined. Thus, an


BlooP and FlooP and GlooP 413
Free download pdf