title: Lua tail-call vs loop
body: <p>I'm writing a little CLI module in Lua to be embedded in a C program.</p>
  
  <p>I was wondering what is the best approach to handle the prompt, choosing between <strong>tail-call</strong> and <strong>loop</strong>.</p>
  
  <p>As <strong>tail-call</strong> I would do something like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I would ask the following questions:</p>
  
  <ol>
  <li><p>Is it a correct use/implementation of <em>tail-call elimination</em>? Does <code>call[cmd] ()</code> introduce any disturbance in the stack, so that I won't take advantage of the <em>tail-call elimination</em>?</p></li>
  <li><p>Is it better using a <strong>loop</strong> like the following? If yes, why?</p>
  
  <pre><code>repeat
    io.write ('% ')
    local cmd = io.read()
  
    -- do stuff
  until cmd == 'quit'
  </code></pre></li>
  <li><p>In Programming in Lua is stated</p>
  
  <blockquote>
    <p>A <em>tail call</em> is a goto dressed as a call.</p>
  </blockquote>
  
  <p>so is there any concrete difference between a tail call and a loop?</p></li>
  </ol>
  
  <p>Thank you.</p>