Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:20:50+00:00 2026-06-08T07:20:50+00:00

I’m writing a little CLI module in Lua to be embedded in a C

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:20:54+00:00Added an answer on June 8, 2026 at 7:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.