Polyglot: Lua (Part 1)

In my previous post, I talked about the reasons why I want to learn more programming language, the Lua programming language, and the developer tooling for Lua. Now it’s time to actually code in Lua!

For this post, I’ll be completing several basic Rose…


This content originally appeared on DEV Community and was authored by Nicholas Synovic

In my previous post, I talked about the reasons why I want to learn more programming language, the Lua programming language, and the developer tooling for Lua. Now it's time to actually code in Lua!

For this post, I'll be completing several basic Rosetta Code tasks. Nothing crazy, but enough to get me familiar with the language and its syntax. As Lua has a fairly minimal and straight forward syntax, I'll post the code snippets and output here, but I won't explain the implementation. For the complete source code, you can see my GitHub repository here.

GitHub Template

I created a GitHub Template to bootstrap my Lua projects going forward. You can find it here. As I find tooling to improve my Lua experience, I'll update the template.

Rosetta Code Problems

Integer Arithmetic

Outcome: Taught me how to take in user input and function declarations

local function sum(a, b)
    return a + b
end

local function difference(a, b)
    return a - b
end

local function product(a, b)
    return a * b
end

local function int_quotient(a, b)
    return a // b -- Rounds to negative infinity
end

local function remainder(a, b)
    return a % b
end

local function exponentiation(a, b)
    return a ^ b
end

local function main()
    io.write("First number: ") -- Use with io.read for single line input
    local a = io.read("n") -- Captures user input

    io.write("Second number: ")
    local b = io.read("n")

    print("===")

    print("Sum: " .. sum(a, b)) -- ".." syntax used to concatenate
    print("Difference: " .. difference(a, b))
    print("Product: " .. product(a, b))
    print(
        "Integer Quotient (rounds to negative infinity): " .. int_quotient(a, b)
    )
    print("Remainder" .. remainder(a, b))
    print("Exponentiation: " .. exponentiation(a, b))
end

main()

String Length Comparison

Outcome: Learned that all objects (including arrays) are tables, how to sort tables, and how to index over them with a for loop

local function main()
    io.write("First string: ")
    local a = io.read("l")

    io.write("Second string: ")
    local b = io.read("l")

    io.write("Third string: ")
    local c = io.read("l")

    print("===")

    local strings = { a, b, c } -- Loads strings into an array (implemented as a table)

    table.sort(strings, function(foo, bar)
        return #foo > #bar
    end) -- Sort array based on string length
    for _, s in ipairs(strings) do
        print(#s, s) -- Print string size then string content
    end
end

main()

Conclusions

Lua wasn't that hard to get a basic grasp of. While yes, I did not cover aspects such as loops, control flow, or binary operations, reading the manual and book provided enough context for me to grasp the core concepts.

I'd like to thank the Rosetta Code community for their problems and solutions. Without them it would be far more difficult for me to understand these core language features.


This content originally appeared on DEV Community and was authored by Nicholas Synovic


Print Share Comment Cite Upload Translate Updates
APA

Nicholas Synovic | Sciencx (2025-01-13T00:06:28+00:00) Polyglot: Lua (Part 1). Retrieved from https://www.scien.cx/2025/01/13/polyglot-lua-part-1/

MLA
" » Polyglot: Lua (Part 1)." Nicholas Synovic | Sciencx - Monday January 13, 2025, https://www.scien.cx/2025/01/13/polyglot-lua-part-1/
HARVARD
Nicholas Synovic | Sciencx Monday January 13, 2025 » Polyglot: Lua (Part 1)., viewed ,<https://www.scien.cx/2025/01/13/polyglot-lua-part-1/>
VANCOUVER
Nicholas Synovic | Sciencx - » Polyglot: Lua (Part 1). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/13/polyglot-lua-part-1/
CHICAGO
" » Polyglot: Lua (Part 1)." Nicholas Synovic | Sciencx - Accessed . https://www.scien.cx/2025/01/13/polyglot-lua-part-1/
IEEE
" » Polyglot: Lua (Part 1)." Nicholas Synovic | Sciencx [Online]. Available: https://www.scien.cx/2025/01/13/polyglot-lua-part-1/. [Accessed: ]
rf:citation
» Polyglot: Lua (Part 1) | Nicholas Synovic | Sciencx | https://www.scien.cx/2025/01/13/polyglot-lua-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.