0 оценок0% нашли этот документ полезным (0 голосов)

6 просмотров6 страницdouglas hofstadter

© © All Rights Reserved

PDF, TXT или читайте онлайн в Scribd

douglas hofstadter

© All Rights Reserved

0 оценок0% нашли этот документ полезным (0 голосов)

6 просмотров6 страницdouglas hofstadter

© All Rights Reserved

Вы находитесь на странице: 1из 6

initions, one for each power, connect-

ing it to the preceding power. Thus a

THEMAS typical floor in this tower would be:

(def 102nd-power

(lambda (q)

(times q (lOlst-power q))»

Tripping the light recursive in Lisp,

Of course, lOlst-power would refer to

the language ofartificial intelligence lOOth-power in its definition, and so on,

thereby creating a rather long regress

back to the "embryonic," or simplest,

case. Incidentally, that very simplest

by Douglas R. Hofstadter case, rather than "square" or even "Ist-

power", is this:

I ended last month's column are placed to the left of their operands,

Since with a timely newsbreak about the inside parentheses. We get something I told you that you had all the informa-

homely Glazunkian porpuquine, I like this:

felt it onlyfitting to start off this month's

tion necessary to assemble the proper

definition. All you needed to observe is,

with more about that little-known but (buying-power q s) is: of course, that each floor of the tower

remarkable beast. As you may remem- cond (eg s 0) 1; rests on the "next-smaller" floor (except

ber, the quills on any porpuquine (ex- t (times for the bottom floor, which is a "stand-

cept the tiniest ones) are smaller porpu- q alone" floor). By "next-smaller" I mean

quines. The tiniest porpuquines have no (buying-power the following:

quills but do have a nose, and a very q

important nose at that, since the Gla- (next-smaller s))) (def next-smaller

zunkians base their entire monetary sys- (lambda (s) (difference s 1)))

tem on that little object. Consider the This is an exact translation of the earlier

value of three-inch porpuquines in Out- English recipe into a slightly more sym- Thus "(next- smaller 102)" yields 101.

er Glazunkia. Each porpuquine always bolic form. We can make it a little more Actually Lisp has a standard name for

has nine quills (contrasting with its cous- compact and symbolic by adopting a this, namely "subl", and a name for its

ins in Inner Glazunkia, which always couple of new conventions. Let each of inverse as well, namely "addl". If we

have seven); thus each has ninetwo-inch the two cases (the case where s equals 0 put all our observations together, we

porpuquines sticking out of its body. and the "otherwise" case) be enclosed in come up with the following universal

Each of those in turn sports nine one- parentheses; in general, use parentheses definition:

inch porpuquines, out of each of which to enclose each logical unit completely.

sprout nine zero-inch porpuquines, each Finally, indicate by the words "def" and (def power

of which has one nose. All told this "lambda" that this is a definition of a (lambda (q s)

comes to 9X9X9XI noses, which general notion called "buying power" (cond ((eq s 0) 1)

means that a three-inch porpuquine in with two variables (quill count q and (t

Outer Glazunkia has a buying power of size s). Now we get: (times q

729 noses. If, in contrast, we had been in (power q

Inner Glazunkia and had started with (def buying-power (next-smaller s)))))))

a four-incher, that porpuquine would (lambda (q s)

have a buying power of 7 X 7 X 7 X (cond ((eq s 0) 1) This is the answer to the puzzle I posed.

7 X 1, or 2,401, noses. (t H'm, that's funny. I have the strangest

Let us see if we can come up with a (times q sense of deja vu. I wonder why?

generalrecipe for calculating the buying (buying-power q

definition presented here is called

power (measured in noses) of any por-

puquine. It seems to me that it would go

(next-smaller s)))))))

The a recursive definition, for the rea-

something like this: We mentioned above that the buying son that inside the definiens the defini-

power of a nine-quill, three-inch porpu- endum is used. This is a fancy way of

The buying power of a porpuquine with quine is 729 noses. This could be ex- saying that I appear to be defining

a given quill count and size is: pressed by saying that (buying-power 9 something in terms of itself, which

if its size equals 0, then 1; 3) equals 729. Similarly, (buying-power ought to be considered gauche if not

otherwise figure out the buying pow- 7 4) equals 2,401. downright circular in anyone's book. To

er of a porpuquine with the same see whether the Lisp genie looks askance

quill count but of the next-smaller let us get back to Lisp. I had on such trickery let us ask it to figure out

size and multiply that by the quill

count.

Now posed a puzzle toward the end of (power 9 3):

last month's column in which the object

was to write a Lisp function that sub- — > (power 9 3)

We can shorten this recipe by adopting sumed a family of functions called 729

some symbolic notation. First, let "q" "square", "cube", "4th-power", "sth- —>

stand for the quill count and "s" for power" and so on. I asked you to come

the size. Then let "cond" stand for "if" up with one general function called Well, fancy that! No complaints? No

and "t" for "otherwise." Finally, use a "power", having two variables, such choking? How can the Lisp genie swal-

kind of condensed algebraic notation in that "(power 9 3)" gives 729, "(power low such nonsense?

which the English names of operations 7 4)" gives 2,401 and so on. I had pre- The best explanation I can give is to

22

point out that no circularity is actual- but the Lisp genie who must keep track That's easy: When n equals 0, our list

ly involved. Although it is true that of these things. The Lisp genie has to be should be empty, which means the an-

the definition of "power" uses the word able to suspend one computation to swer is nil. We can now put our observa-

"power" inside itself, the two occurren- work on another one whose answer was tions together as follows:

ces are referring to different circum- asked for by the first one. And the sec-

stances. In a nutshell, (power q s) is be- ond computation too may ask for the (def bunch-of-stones

ing defined in terms of a simpler case, answer to a third one, thus putting itself (lambda (n)

namely (power q (next-smaller s)). Thus on hold, as may the third, and so on (cond ((eq n 0) nil)

I am definingthe 44th power in terms of recursively. Eventually, however, there (t

the 43rd power, and that in terms of the will come a case where the buck stops— (cons 'stone

next-smaller power, and so on downthe that is, where a process runs to comple- (bunch-of-stones

line until we come to the "bottom line," tion and returns a value— and this will (next-smaller n)))))))

as I call it: the othpower, which needs noenable other stacked-up processes to fi-

nally return values, like stacked-up air- Now let us watch the genie put togeth-

recursion at all. It suffices to tell the ge-

nie that its value is 1, which we did. So planes that have circled for hours finally er a very small bunch of stones (with

when you look carefully, you see that getting to land, with each landing open- "trace" on, just for fun). For this you

this recursive definition is no more cir- ing the way for another landing. will need to refer to the illustration on

cular than the "tower of power" was— Ordinarily the Lisp genie will not the opposite page.

and you cannot get any straighter than print out a trace of what it is thinking This illustrates what is called "cons-

unless you askfor it. Whether you ask to ing up a list." Now let us try another

an infinite straight line! In fact, this one

compact definitionreally is just a way of see it or not, however, this kind of thing one. This one is an old chestnut of Lisp

getting the entire tower of power into is goingon behind the scenes whenever a and indeed ofrecursion in general. Look

one finite expression. Far from being function call is evaluated. One of the at the definition and see whether you can

circular, it is just a handy summary of enjoyable things about Lisp is that it figure out what it is supposed to do; then

infinitely many different definitions, allcan deal with such recursive definitions read on to see if you were right.

belonging to one family. without getting flustered.

In case you still have a trace of skepti- (def wow

am not so naive as to expect that you (lambda (n)

cism about this sleight of hand, perhaps

I should let you watch what the Lisp I

have now totally got the hang of re- (cond ((eq n 0) 1)

genie will do if you askfor a "trace" of cursion and could go out and write huge (t

the function and then ask it once again recursive programs with ease. Indeed, (times n (wow (subl n)))))))

to evaluate (power 9 3). For this you willrecursion can be a remarkably subtle

need to refer to the illustration below. means of defining functions, and some- Remember, "subl" means the same as

On the lines marked "ENTERING" times even an expert will have trouble "next-smaller". For a lark, why don't

the Lisp genie prints the values of the figuring out the meaning of a compli- you calculate the value of (wow 100)?

two arguments, and on the lines marked cated recursive definition. I therefore (If you ate your mental Wheaties this

"EXITING" it prints the value it has thought I would give you some practice morning, try it in your head.)

computed and is returning. For each in working with recursion. It happens that Lisp genies often

ENTERING line there is of course an Let me give a simple example based mumble out loud while they are execut-

EXITING line, and the two are aligned on this riddle: How do you make a pile ing wishes, and I just happen to have

vertically, that is, they are indented by of 13 stones? A silly answer would be: overheard this one as it was executing

the same amount. Put one stone on top of a pile of 12 the wish "(wow 100)". Its soliloquy ran

You can see that in order to figure out stones. Suppose we want to make a Lisp something like this:

what (power 9 3) is, the genie must first function that will give us not a pile of 13 "H'm...(wow 100), eh? Well, 100

calculate (power 9 2). This, however, is stones but a list consisting of 13 copies surely isn't equal to 0, so I guess the

not a given; instead it requires knowing of the atom "stone" or, in general, n cop- answer has to be 100times what it would

the value of (power 9 1), and this in turn ies of that atom. We can base our answer have been had the problem been (wow

requires (power 9 0). Ah! We were giv- on the riddle's silly-seeming yet correct 99), All right—now all I need to do is

en this one: it is just 1. And now we recursive answer. The general notion is figure out what (wow 99) is. Oh, this is

can bounce back "up," remembering to build the answer for n out of the an- going to be a piece of cake! Let's see,

that in order to get one answer from the swer for n's predecessor. Build how? Us- is 99 equal to 0? No, seems not to be,

"deeper" answer we must multiply by 9. ing the list-building function cons, that's so I guess the answer to this problem

Hence we get 9, then 81, then 729, and how. What is the embryonic case? That must be 99 times what the answer

we are done. is, for which value of n does this riddle would have been had the problem been

I say "we," but of course it is not we present absolutely no problem at all? (wow 98). Let's see ..."

At this point the author, having some

pressing business at the bank, had to

leave the happy genie and did not re-

—> (power 9 3)

ENTERING power (q = 9, s - 3)

ENTERING power (q = 9, s = 2)

turn until some milliseconds afterward.

When he did so, the genie was justfinish-

ing up, saying:

ENTERING power (q = 9, s = 1) "... and now I just need to multiply

ENTERING power (q = 9, s = 0) that by 100, and I've got my final an-

EXITING power (value: 1) swer. Easy as pie! I believe it comes out

1

EXITING power (value: 9) to be 93326215443944152681699238-

EXITING power (value: 81) -856266700490715968264381621468-

EXITING power (value: 729) 5-92963895217599993229915608941-

729 46-3976156518286253697920827223-

—> 758-251185210916864000000000000-

0000-0000000o—if I'm not mistaken."

Is that the answer you got, dearread-

The Lisp genie evaluates (power 9 3) er? No? Oh, I see where you went wrong.

26

It was in your multiplication by 52. Go

back and try it again from that point on, — > (bunch-of-stones 2)

and be a little more careful in adding up ENTERING bunch-of-stones (n = 2)

those long columns. I'm quite sure you'll ENTERING bunch-of-stones (n = 1)

get it right this time. ENTERING bunch-of-stones (n = 0)

EXITING bunch-of-stones (value: nil)

function is ordinarily

Thiscalled"wow"

"factorial"; n factorial is usu-

EXITING

EXITING

bunch-of-stones (value: (stone))

bunch-of-stones (value: (stone stone))

ally defined as the product of all the (stone stone)

numbers from 1 through n. A recursive

definition, however, looks at things a bit —>

differently: speaking recursively, n fac-

torial is simply the product of n and the The genie puts together a very small bunch ofstones

preceding factorial. It reduces the given

problem to a simpler one of the same

type. That simpler one will in turn be As it turns out, this is a most reason- way is quite obviously linear. For in-

reduced, and so on down the line, until able assumption to make in all kinds of stance, it is obvious that by subtracting

you come to the simplest problem of circumstances. To spell out the exact na- 1 repeatedly, you will eventually reach

that type, which I call the embryonic ture of this recursion-guiding pathway 0, provided you started with a positive

case or the bottom line. People speak, you have to answer two Big Questions: integer. It is also obvious that by per-

in fact, of a recursion "bottoming out." forming the list-shortening operation of

A New Yorker cartoon from a few 1. What is the embryonic ease? "cdr" you will eventually reach nil, pro-

years back illustrates the concept per- 2. What is therelation between a typi- vided you started with a finite list. For

fectly. It shows a man of 50 or so hold- cal case and the next-simpler case? this reason recursions using "subl" or

ing a photograph of himself taken "cdr" to define their pathway of descent

roughly 10 years earlier. In that photo- Now, actually both of these Big Ques- toward the bottom are commonplace. I

graph he is holding a photograph of tions break up into two subquestions (as shall present a cdr-based recursion be-

himself 10 years earlier than that. And befits any self-respecting recursive ques- low, but first I want to show a funny

on it goes, until eventually it bottoms tion), one concerning howyou recognize numerical recursion in which the path-

out—quite literally—in a photograph of where you are or how to move, the other way toward the embryonic case is any-

a bouncy baby boy in his birthday suit concerning what the answer is at any thing but linear and smooth.

(bottom in the air). This idea of recur- given stage. Thus, spelled out more ex-

the famous "3n +1" prob-

sive photographs catching you as you

grow up is quite appealing— I wish my

plicitly, our Big Questions are:

Consider

lem, in which you with any

start

parents had thought of it. Compare it la. How can you know when you positive integer, and if it is even, you

with the more famous infinite regress have reached the embryonic case? halve it; otherwise you multiply it by 3

on the Morton Salt package, where the lb. What is the embryonic answer? and add 1. Let us call the result of this

Morton Salt girl holds a box of Morton operation on n "(hotpo n)" (standing

Salt with her picture on it. Since the girl la. From a typical case, how do you for "half or triple plus one"). Here is a

in the picture is no younger, however, take exactly one step toward the embry- Lisp definition of hotpo:

there is no bottom line and the regress onic case?

is—at least theoretically—endless. 2b. How do you build this case's an- (def hotpo

The recursive approach works when swer out of the "magically given" an- (lambda (n)

you have a family of related problems at swer to the simpler case? (cond ((even n) (half n))

least one of which is so simple that it can (t(addl (times 3 n))))))

be answered immediately. This I callthe Question 2a concerns the nature of the

embryonic case. (In the factorial exam- descent toward the embryonic case, or This definition presumes that two other

ple that is the "(eq n 0)" case, whose bottom line. Question 2b concerns the functions either have been or will be

answer is 1.) Each problem (for in- inverse aspect, namely the ascent that defined elsewhere for the Lisp genie,

stance, "What is 100 factorial?") can be carries you back up from the bottom to namely "even" and "half" ("addl" and

viewed as a particular case of one gener- the top level. "times" being, as I mentioned earlier,

al problem ("How do you calculate fac- In the case of the factorial the answers intrinsic parts of Lisp). Here are the

torials? "). Recursion takes advantage of to the Big Questions are: missing definitions:

the fact thatthe answers to various cases

are related in some logical way to one la. The embryonic case occurs when (def even

another. (For example, I could very eas- the argument is 0. (lambda (n) (eg (remainder n 2) 0)))

ily tell you the value of 100 factorial if lb. The embryonic answer is 1.

only someone would hand me the value (def half (lambda (n) (quotient n 2)))

of 99 factorial; all I need to do is multi- 2a. Subtract 1 from the present ar-

ply by 100.) You could say that the re- gument. What do you think happens if you be-

cursioneer's motto is "Gee, I could solve 2b. Multiply the "magic" answer by gin with some integer and perform hot-

this case if only someone would magi- the present argument. po over and over again? Take 7, for in-

cally hand me the answer to the case that stance, as your starting point. Before

is one step closer to the embryonic Notice how the answers to these four you do the arithmetic guess at what kind

case." Of course, this motto presumes questions are all incorporated in the re- of behavior might result.

that certain cases are in some sense cursive definition of "wow". As it turns out, the pathway is often

"nearer" to the embryonic case than Recursion relies on the assumption surprisingly chaotic and bumpy. For in-

others. In fact, it presumes that there is a that sooner or later you will bottom out. stance, if we begin with 7, the process

natural pathway leading from any case One way to be sure you will bottom out leads us to 22, then 11, then 34, 17, 52,

through simpler cases all the way down is to have all the "descending," or sim- 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1,

to the embryonic case, a pathway whose plifying, steps move in the same direc- 4, 2, 1, ...Note that we wind up in a

steps' are clearly marked. tion at the same rate, so that your path- short loop. Suppose, therefore, we agree

27

It is easy: add the number of unicorns

in the Estremadura region (Portugal's

— > (pathway-to-1 3)

ENTERING pathway-to-1 (tato =3) "car") to the number of unicorns in the

ENTERING pathway-to, 1 (tato = 10) rest of Portugal (Portugal's "cdr"). And

ENTERING pathway-to-1 (tato =5) how do you count up the unicorns in

ENTERING pathway-to- 1 (tato =16) Estremadura (not to mention those in

ENTERING pathway-to- 1 (tato =8) the remaining regions of Portugal)?

ENTERING pathway-to-1 (tato == 4) By further breakup, of course. What,

ENTERING pathway-to-1 (tato =2) then, is the bottom line? Well, regions

ENTERING pathway-to-1 (tato = 1) can be broken up into districts, districts

EXITING pathway-to-1 (value: (1)) into square kilometers, square kilome-

EXITING pathway-to-1 (value: (2 1)) ters into hectares, hectares into square

EXITING pathway-to-1 (value: (4 2 1)) meters—and we can handle each square

EXITING pathway-to-1 (value: (8 4 2 1)) meter without further breakup.

EXITING pathway-to-1 (value: (16 8 4 2 1)) Although this may soundrather ardu-

EXITING pathway-to-1 (value: (5 16 8 4 2 1)) ous, there really is no way to conduct a

EXITING pathway-to-1 (value: (10 5168 4 2 1)) thorough census other than to traverse

EXITING pathway-to-1 (value: (3 10 5168 4 2 1)) every single part on every level of the

(3 10 5 16 8 4 2 1) full structure you have, no matter how

—> giant it may be.. There is a perfect Lisp

counterpart to this unicorn census: it is

the problem of determining how many

How the genie "thinks" when the trace feature is on atoms there are inside an arbitrary list.

How can we write aLisp function called

"atomcount" that will give us the an-

that if weever reach 1, we have hit bot- way belonging to (hotpo 7), or 22. After swer 15 when it is shown the following

tom and can stop. You might well ask, all, 22 is one step closer to being embry- strange-looking list (which we shall call

"Who says we shall hit 1? Isthere a guar- onic than 7 is! "brahma")?

antee?" Indeed, if you haven't tried it These answers enable us to write

outT a bit, there is no obvious reason to down the desired function definition, (((ac ab eb) ac (ba be ac))

suspect that you will hit 1. (In the case using "tato" as our dummy variable — ab

of 7 did you suspect it would happen?) "tato" being a well-known acronym for ((cb ca ba) eh (ac ab cb)))

Numerical experimentation, however, "tato (and tato only)", which recursively

reveals a remarkable reliability to the expands to "tato (and tato only) (and One method, expressed recursively, is

process; it seems that no matter where tato (and tato only) only)", and so forth. exactly parallel to that for ascertaining

you start, you always do hit 1 after a the unicorn population of Europe. See if

while. (Try startingwith 27 if you want a (def pathway-to-1 you can write it down.

real roller-coaster ride!) (lambda (tato)

idea is this. We con-

Can you write arecursive function to

reveal the pathway followed from anar-

(cond ((eq tato 1) '(1))

(t The struct

want to

the answer—namely 15—

ofthe answers simpler atom-counting

out

bitrary starting point "down" to 1? Note (cons tato to

that I say "down" advisedly, since many (pathway-to- 1 problems. Well, it is obvious that one

of the steps are in fact up. Thus the path- (hotpo tato))))))) atom-counting problem simpler than

way starting at 3 would be the list "(3 10 (atomcount brahma) is (atomcount (Gar

5 16 8 4 2 1)". In order to solve this Look at the way the Lisp genie "thinks" brahma)). Another is (atomcount (cdr

puzzle you need to go back and answer (as revealed when the trace feature is brahma)).: The. answers to these two

for yourself the two Big Questions of on). Here you should refer to the illus- problems are respectively 7 and 8. Now,

Recursion, as they apply here. Note: tration above. clearly 15 is made out of 7 and 8 by

Notice the total regularity (the V addition— which makes sense, after all,

(cond ((not (want help)) shape) of the left margin of the trace since the total number of atoms must be

(not (read further))) diagram, in spite of the chaos of the the number in the car plus the number in

(t (read further))) numbers involved. Not all recursions the cdr. There is nowhere else for any

are so geometrically pretty when they atoms to hide. This analysis gives us the

about the embryonic case. This is are traced. The reason is that some following recursive definition, with "s"

First,

easy. It has already been defined problems require that more than one sub- as the dummy variable:

as the arrival at 1, and the embryonic, problem be solved. As a practical; real-

or simplest possible, answer is the list life example of such a problem, consider (def atomcount

"(1)", a tiny but valid pathway begin- howyou might go about counting up all (lambda (s)

ning and ending at 1. the unicorns in Europe. This is certainly (plus (atomcount (car s))

Second, about more typical cases. a nontrivial undertaking, yet there is an (atomcount (cdr s)))))

What operation will carry us from typi- elegant recursive answer: Count up all

cal 7 one step closer to embryonic 1? the unicorns in Portugal (the "car" of It looks simple, but it has a couple of

Certainly not the "subl" operation. No, Europe, metaphorically speaking), then flaws. First, we have written the recursive

by definition it is the function hotpo it- count up all the unicorns in the other 30- part of the definition, but we have ut-

self that brings you ever "nearer" to 1— -odd countries of Europe (the "cdr" of terly forgotten the other equally vital

even when it carries you up. This teasing Europe, to continue the metaphor) and —

half the bottom line.Jt reminds me of a

quality is of course the entire point of finally add the two results together. Maryland judgeI once read about in the

the example. What about 2b, how to re- Notice how this spawns two smaller paper, who ruled that "a horse is a four-

cursively build a list documenting our unicorn-counting subproblems, which legged animal that is produced by two

wildly oscillating pathway? Well, the in turn will spawn two subproblems other horses." This is a lovely definition,

pathway belonging to 7 is got by tacking each, and so on. For instance, how can but where does it bottom out? It is the

(that is, consing) 7 onto the shorter path- one count all the unicorns in Portugal? same for atomcount. What is the, sim-

28

plest case, the embryonic case, of atom- "(a b c)", instead of getting 3 for an an- (plus (atomcount (car s))

count? It is when we are asked to count swerve shall get 4. Shocking! How does (atomcount (cdr s)))))))

the atoms in a single atom. The answer this happen? Well, we can pin the prob-

.

in such a case is of course 1 But how can lem down by trying an even simpler ex-

ample: if we ask for (atomcount '(a)),

I wrote "(null s)", which is just another

way of saying "(eq s nil)". In general if

we know when we are looking at an

atom? Fortunately Lisp has a built-in we find we get 2 instead of 1. Now the er- you want to determine whether the val-

function called Jiatom" that returns t ror sliould'be clearer: 2=l+l, with 1 ue of some expression is nil or not, you

(meaning "true") whenever we are look- each coming from the car and the cdr of can use the inbuiltfunction "null", which

ing at an atom, and returns nil other- "(a)". The car is the atom "a", which returns tif yes arid nil if no. Hence," for

wise. Thus "(atom 'plop)" returns t and indeed should be counted as 1, but the example, "(null (null nil))" evaluates to

"(atom '(a be))" returns nil. Using that, cdr is nil, which should not. Then why nil, since the inner function call evalu-

we can patch up our definition: does nil give an atomcount of 1? Be- ates to t, and tis not nil! What about

cause nil is not only an empty list but "(null '(null nil))"?

(def atomcount also an atom! To suppress this bad effect

(lambda (s) we simply insert another cond clause at HVTow, what happens when we rurLOur

(cond ((atom s) 1) the very top:

top -LN. function on brahma, its original

(t ■target? The result, with trace on, is

(plus (atomcount (car s)) (def atomcount

atomco shown below.

(atomcount (cdr s))))))) (lambda

(lambda (s) Notice the more complicated topog-

(cond ((null s) 0) raphy of this recursion, with its ins and

It is still, however, not quite right. If ((atom s) 1) outs along the left margin. The preced-

we ask the genie for the atomcount of (t ing F-shaped recursion looked like a

ENTERING atomcount ENTERING atomcount (s - (cb ca ba))

(s = (((ac ab cb) ac (ba be ac)) ab ((cb ca ba) cb (ac ab cb)))) ENTERING atomcount (s = cb)

ENTERING atomcount (s - ((ac ab cb) ac (ba be ac))) EXITING atomcount (value: 1)

ENTERING atomcount (s = (ac ab cb)) ENTERING atomcount (s = (ca ba))

ENTERING atomcount (s = ac) ENTERING atomcount (sea) = ca)

. EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = (ab cb)) ENTERING atomcount (s = (ba))

ENTERING atomcount (sab) = ab) ENTERING atomcount (s = ba)

EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = (cb)) ENTERING atomcount (s = nil)

ENTERING atomcount (s = cb) EXITING atomcount (value: 0)

EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = nil) EXITING atomcount (value: 2)

EXITING atomcount (value: 0) EXITING atomcount (value: 3.)

EXITING atomcount (value: 1) ENTERING atomcount (s = (cb (ac ab cb)))

EXITING atomcount (value: 2) ENTERING atomcount (s = cb)

EXITING atomcount (value: 3) EXITING atomcount (value: 1)

'ENTERING atomcount (s = (ac (ba be ac))) ENTERING atomcount (s = ((ac ab cb)))

ENTERING atomcount = (s ac) ENTERING atomcount (s = (ac ab cb))

EXITING atomcount (value: 1) ENTERING atomcount (s = ac)

ENTERING atomcount (s = ((ba be ac))) EXITING atomcount.(value: 1)

ENTERING atomcount (s = (ba be ac)) ENTERING atomcount (s = (ab cb))

ENTERING atomcount (s = ba) ENTERING atomcount (sab) = ab)

EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = (be ac)) ENTERING atomcount. (s = (cb))

ENTERING atomcount (s = be) ENTERING atomcount (s = cb)

EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = (ac)) ENTERING atomcount (s = nil)

ENTERING atomcount (s = ac) EXITING atomcount (value: 0)

EXITING atomcount (value: 1) EXITING atomcount (value: 1)

ENTERING atomcount (s = nil) EXITING atomcount (value: 2)

EXITING atomcount (value: 0) EXITING atomcount (value: 3)

EXITING atomcount (value: 1) ENTERING atomcount (s nil) —

EXITING atomcount (value: 2) EXITING atomcount (value: 0)

EXITING atomcount (value: 3) EXITING atomcount (value: 3)

ENTERING atomcount (s.= nil) EXITING atomcount (value: 4)

EXITING atomcount (value: 0) EXITING : atomcount (value: 7)

EXITING atomcount (value: 3) ENTERING atomcount (s = nil)

EXITING atomcount (value: 4) EXITING atomcount (value: 0)

EXITIN G atomcount (value: 7) EXITING atomcount (value: 7)

ENTERING atomcount EXITING atomcount (value: 8)

(s = (ab ((cb ca ba) cb (ac ab cb)))) E

EXITING atomcount (value: 15)

ENTERING atomcount (sab) = ab) 15

EXITING atomcount (value: 1) — >

ENTERING atomcount

(s = (((cb ca ba) cb (ac ab cb))))

29

simple descent into a smooth-walled world all 64 disks formed the Tower of

canyon and then a simple climb back up Brahma on one needle. Now, however,

the other side; this recursion looks like a the process of transfer of the tower from

descent into a craggier canyon, where one needle to another is in midcourse.

on your way up and down each wall When the last disk is finally in place,

you encounter various "subcanyons" once again forming the Tower of Brah-

that you must treat in the same way— ma but on a different needle, then will

and who knows how many levels of come the end of the world and all will

such structure you will be called on turn to dust.

to deal with in your exploration? The A picture of the puzzle is shown be-

shape described by a structure that goes low; I have labeled the three needles "a",

on indefinitely like that has been named "b" and "c".

a "fractal" by Benoit Mandelbrot. If you work at it, you certainly can

Notice in this recursion that we have discover the systematic method the

more than one type of embryonic case priests must follow to get the disks from

(the "null" case and the "atom" case) needle "a" to needle "b". For only three

and more than one way of descending disks, for instance, it is not difficult

toward the embryonic case (by way of to write down the order in which the

both car and cdr). Thus our Big Ques- moves go:

tions can be revised a bit further:

ab ac be ab ca cb ab

la. Is there just one embryonic case,

or are there several cases, or is there Here the Lisp atom "ab" represents a

even an infinite class of them? jump from needle "a" to needle "b".

There is a structure to what is going

lb. How can you know when you

have reached an embryonic case? on, however, that is not revealed by a IM

Ic. What are the answers to the vari- mere listing of such atoms. It is bet-

ous embryonic cases? ter revealed if one groups the atoms

as follows:

ab ac be ab ca cb ab

/cm

ly one way to step toward an embryonic

case, or are there various possibilities? $tm

2b. From a typical case how do you The first group of three accomplishes a

determine which of the various routes transfer of a 2-tower from needle "a" to

toward an embryonic case to take? needle "c", thereby freeing up the larg-

2c. How do you build this case's an- est disk. Then the middle move, "ab",

swer out of the "magically given" an- picks up that big, heavy disk and carries

swers to one or more simpler cases? it over from needle "a" to needle "b".

The final group of three is much like the

of the elegant recursions

most I initial group in that it transfers the 2-

Oneknow of originates with the fa-

puzzle known vari-

tower back from needle "c" to needle

"b". Thus the solution to moving three

mous disk-moving

ously as "Lucas's Tower," the "Tower disks depends on being able to move

of Hanoi" and the "Tower of Brahma." two. Similarly, the solution to moving

Apparently it was originated by the 64 disks depends on beirig able to move

French mathematician Edouard Lucas 63. Enough said? Now try to write a Lisp

in the 19th century. The legend that is function that will give you a solution

popularly attached to the puzzle goes to the Tower of Brahma for n disks.

like this: (You may prefer to label the three nee-

In the great temple of Brahma in Be- dles with digits rather than letters, so

nares, on a brass plate under the dome that moves are represented by two-digit

that marks the center of the world, there numbers such as 12.) I shall present the

are 64 disks of pure gold that the priests solution next month—unless, of course,

carry one at a time between three dia- the dedicated priests, working by day

mond needles according to Brahma's and by night to bring about the end of V

immutable law: No disk may be placed the world, should chance to reach their

on a smaller disk. In the beginning of the cherished goal before then.

lw

us

The Tower of Brahma puzzle. The 64 disks on "a" must be placed in the same order on "b"