Question

I'm writing a little CLI module in Lua to be embedded in a C program.

I was wondering what is the best approach to handle the prompt, choosing between tail-call and loop.

As tail-call I would do something like this:

call = { help=function () print 'just ask politely' end }

function shell ()
  io.write ('% ')
  local cmd = io.read ()

  if cmd ~= 'quit' then      
    call[cmd] () -- for simplicity assume call[cmd] is never nil
    return shell ()
  end
end

I would ask the following questions:

  1. Is it a correct use/implementation of tail-call elimination? Does call[cmd] () introduce any disturbance in the stack, so that I won't take advantage of the tail-call elimination?

  2. Is it better using a loop like the following? If yes, why?

    repeat
      io.write ('% ')
      local cmd = io.read()
    
      -- do stuff
    until cmd == 'quit'
    
  3. In Programming in Lua is stated

    A tail call is a goto dressed as a call.

    so is there any concrete difference between a tail call and a loop?

Thank you.

Was it helpful?

Solution

Is it a correct use/implementation of tail-call elimination?

If you're asking if the call to shell at the end is a proper tail call according to Lua syntax, the answer is yes.

Does call[cmd] () introduce any disturbance in the stack, so that I won't take advantage of the tail-call elimination?

A function call does not modify the stack in the manner in which you're thinking. In Lua, the only requirement of a tail call is that it be of the form return Function(params), with no extra return values that aren't the return values from the function.

A proper tail call does not even have to call itself; it doesn't need to be recursive.

Is it better using a loop like the following? If yes, why?

That's a subjective point. Personally, I'd say that the loop is more clear on what's going on.

However, if you want an objective question of performance, consider this: the tail call will never be faster than the loop. The absolute best you're going to get in terms of performance is equal.

And it probably won't be that. Lua tail call "optimization" simply means that it reuses the stack entries of the current function. Lua still has to fetch the function from the global table. Lua still has to do all of the overhead of calling a function; it just doesn't have to allocate more stack memory.

It's really about not overflowing the stack and not allocating memory when it's unnecessary.

is there any concrete difference between a tail call and a loop?

See above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top