home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!goanna!ok
- From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
- Newsgroups: comp.programming
- Subject: Re: Teaching the basics
- Message-ID: <14194@goanna.cs.rmit.oz.au>
- Date: 26 Aug 92 10:56:56 GMT
- References: <1992Aug17.123916.14815@husc13.harvard.edu> <7116@charon.cwi.nl>
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Lines: 42
-
- In article <7116@charon.cwi.nl>, tromp@cwi.nl (John Tromp) writes:
- > mjn@pseudo.uucp (Murray Nesbitt) writes:
- > >markh@csd4.csd.uwm.edu (Hunk) writes:
- > >>In article <14066@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
- > >>I think recursion is a farce. And just to rub it in, here's the Towers of
- > >>Hanoi (up to N = 31, I'm assuming long integers are 32 bits) without it (in C)
-
- > Ok, maybe Richard meant something like this:
-
- Please get your attribution straight. That _wasn't_ me. I'm the one who
- said that if you don't understand recursion, you don't understand programming.
-
- > void hanoi(n)
- > {
- > for (x = 1; x < (1<<n); x++)
- > printf("move a disc from %d to %d\n", (x&x-1)%3, ((x|x-1)+1)%3);
- > }
- >
- <some one else>
- > >Beauty is in the eye of the beholder, but in my opinion the recursive
- > >version is a little easier to follow, to say the least.
- >
- > Hmmm, guess I'll have to agree to that:-)
- > But efficient it's not, particularly in its use of (stack) space.
-
- The recursive version of Towers of Hanoi for N disks takes o(N) stack
- frames; a typical stack frame (assuming a good compiler) would be two or
- three words of memory. Using ~2N words of memory for a program that takes
- O(2**N) time doesn't strike _me_ as grossly inefficient.
-
- From my perspective (heavily influenced by Lisp/Scheme/T, NPL/Hope/ML,
- Prolog, and similar languages), the only differences between C's "for"
- and Scheme's "named-LET" are (a) punctuation & spelling and (b) C's
- "for" loop is less expressive for iterations that Scheme's nominally
- recursive (but actually iterative) notation.
-
- Besides, the simple recursive version is not as limited by the size of
- integers. The machine I'm using as a _terminal_emulator_ can go through
- hanoi(32) in under an hour, so don't think that 32-bit integers are enough...
-
- --
- You can lie with statistics ... but not to a statistician.
-