lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Tail-recursion would be cool in general and it would help the break issue 
especially when combined with $macros (once they exist).

If macros (with optional '$', since '$' are ugly) could be made to
implement something like the code below (where begin, again, and break are

  [code before test]
  break(test, loopname)
  [code after test]

this could be turned into

local loopname = 1
while loopname do
  [code before test]
  if test then loopname=nil end
  if loopname then
    [code after test]

Of course the macros would need to get the text of their arguments 
instead of the values in the above code, but that seems like a generally 
useful thing.


On Thu, 2 Sep 1999, Fred Bertsch wrote:

> On Wed, 1 Sep 1999, David Jeske wrote:
> > I'm not concerned with whether the solution involves adding a break
> > statement or not. However, I have to second Fred's complaint about
> > "while()" being the only available looping structure. 
> Here's another possibility that wouldn't require extending the language at
> all.  If you add a tail-recursion optimization, you could make a loop look
> like this:
> local Loop = function( i )
>   [ loop code ]
>   if( i == %endvalue ) then
>     return
>   end
>   [ more loop code ]
>   Loop( i + %increment )
> end
> Loop( startvalue )
> That would give me everything I'd need.  It would also make setting the
> initial value for the loop index and incrementing the loop index be in
> standard locations.  That might make reading code a bit easier.  
> I disagree, by the way, that putting a "break" in the middle of a loop
> creates large numbers of infinite loops.  In Lua, essentially all of the
> infinite loops that I write are because I forgot to write the loop
> index increment code.  (ie: "i = i + 1" or whatever.)  Tail recursion
> would make that much less likely, since in order to make it loop at all
> you'd have to write what should be the loop increment code.
> I would think that tail recursion is a fairly simple optimization to add,
> too.  You just have to check to see if any local variables are used after
> the recursion call and, if not, clean up the stack before making the
> function call.
> F