home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / programm / 2436 < prev    next >
Encoding:
Text File  |  1992-08-23  |  2.8 KB  |  75 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!cs.utexas.edu!torn!utzoo!telly!druid!pseudo!mjn
  3. From: mjn@pseudo.uucp (Murray Nesbitt)
  4. Subject: Re: Teaching the basics
  5. Organization: Private system in Toronto, ON
  6. Date: Sun, 23 Aug 1992 11:19:41 GMT
  7. Message-ID: <MJN.92Aug23031941@pseudo.uucp>
  8. In-Reply-To: markh@csd4.csd.uwm.edu's message of Wed, 19 Aug 1992 15:48:30 GMT
  9. References: <1992Aug17.123916.14815@husc13.harvard.edu> <14066@goanna.cs.rmit.oz.au> <1992Aug19.154830.11787@uwm.edu>
  10. Sender: mjn@pseudo.uucp (Murray Nesbitt)
  11. Lines: 62
  12.  
  13.  
  14. markh@csd4.csd.uwm.edu (Hunk) writes:
  15.  
  16. >In article <14066@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
  17. >>If you don't know recursion, you don't know programming.  It is one of the
  18. >>simpler concepts in computing.  (Much simpler than mutable variables, IMHO.)
  19. >
  20. >I think recursion is a farce.  And just to rub it in, here's the Towers of
  21. >Hanoi (up to N = 31, I'm assuming long integers are 32 bits) without it (in C)
  22.  
  23. You are joking, right?  If not, I don't see how your example Hanoi
  24. program strengthens your position that recursion is a ``farce''.
  25.  
  26. >/* Move a stack of N objects from tower A to C, using B */
  27. >void Hanoi(int N, char A, char B, char C) {
  28. >   unsigned long X = 0; char D;
  29. >HANOI:
  30. >   if (N > 0) {
  31. >      D = B, B = C, C = D, N--;
  32. >      X = 2*X + 2; goto HANOI; X2: X = (X - 1)/2;
  33. >      N++, D = B, B = C, C = D;
  34. >      printf("Move %d from %c to %c.\n", N, A, C);
  35. >      D = A, A = B, B = D, N--;
  36. >      X = 2*X + 1; goto HANOI; X1: X = (X - 1)/2;
  37. >      N++, D = A, A = B, B = D;
  38. >   }
  39. >   if (X&1) goto X1;
  40. >   if (X > 0) goto X2;
  41. >}
  42.  
  43. An equivalent recursive version:
  44.  
  45. void Hanoi( int n, char start, char temp, char dest )
  46. {
  47.     if ( n ) {
  48.         Hanoi( n - 1, start, dest, temp );
  49.         printf( "Move %d from %c to %c.\n", n, start, dest );
  50.         Hanoi( n - 1, temp, start, dest );
  51.     }
  52. }
  53.  
  54. Beauty is in the eye of the beholder, but in my opinion the recursive
  55. version is a little easier to follow, to say the least.
  56.  
  57. On my system, both the native C compiler and gcc with optimization off
  58. produce faster executables for the recursive version (although gcc -O
  59. produces a faster iterative executable; strangely enough, gcc -O
  60. actually slows down the recursive version).
  61.  
  62. On top of it all, the recursive version does not have an arbitrary
  63. limit on the number of disks, and can be easily translated to any
  64. language supporting recursion.
  65.  
  66. For some problems, it may be necessary to avoid (or reduce) recursion
  67. in the interest of execution time.  But generally, if a recursive
  68. solution to a problem is obvious, as is the case with Towers of Hanoi
  69. (or binary tree traversal, etc.), it should be coded recursively.  The
  70. clarity and elegance of a recursive solution will usually outweigh the
  71. (possible) performance penalty.
  72.  
  73. -- 
  74. Murray
  75.