Re: RE:Re: [ROOT] bug&quest

From: Brett Viren (bv@bnl.gov)
Date: Sat Jun 23 2001 - 18:08:21 MEST


Masaharu Goto writes:
 > 
 > However, I personally do not like this kind of programming. 
 > Recursive call can waste physical resources. This small
 > example with a tiny job uses up very deep stack memory.

Just an aside:

The example is tail-recursive so it doesn't need to actually fill up
the stack.  It can be implemented by the interpreter as a loop.  This
is a common optimization in Scheme and Lisp interpreters.  When an
algorithm is inherently recursive, it seems to me that it's valid to
want the code to be as well.

To compare, note the one line addition to `tree()', below, kills
tail-recursiveness and must fill up the stack.  This is because state
of each recursion must be stored until the recursion is finished.

int tree(int x)
{
   static int y=0;
   static int z=0;
   static int qq = printf("%5s","*");

   x = x>0 ? -9: x;
   z = (z=x+5)>0 ? z:-z;
   printf(!x&&++y ? "\n":(z?(z>y%3+y/3?" ":(x<-5 ?"/" :"\\")):"|"));
   int retval = (y-9) ? tree(++x) : printf("  _|_|_\n  \\___/\n");
   fprintf(stderr,"Stack level: %d", x);//<--Kills tail-recursiveness
   return retval;
}


 > This is mostly okey for PCs. But, imagine somebody wants
 > to put this nice picture on a small devices like celler phone
 > where every piece of physical resource is precious.

Root on an iPaq I can believe, but Root on a Nokia?.....

1-800-PHYSICS,
-Brett.



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:50 MET