Basic use of Lua

1. Introduction and links

If you have been following this thread, you will have some understanding of how MS-Windows runs applications and two different ways that you can start up the "lua" application, either by double clicking it and entering commands from the Lua command prompt, or by running "lua53 file.lua" from the MS-Windows command prompt to run the lua program contained in the text file "file.lua".

Both work, and as you get experienced you will find the second much more convenient because you can prepare the commands you want in a separate text file, which prevents you having to type the same commands over and over again. However, when you are learning, the Lua command prompt is very valuable.

The lua command referred to here is "lua53" because it is version 5.3 of Lua. (It is not "lua 5.3" or "lua5.3" because someone very sensibly decided not to use special characters in filenames...) Version 5.3 is the current one at the time of writing. If a more recent version exists by all means use that. 5.1, 5.2, 5.3 or later are all fine for what we will do.

Lua is documented at https://www.lua.org/. In particular there is a useful reference manual, the first edition of which is online and covers version 5.1 of Lua. This is at https://www.lua.org/pil/contents.html. Later editions of this book have to be bought, but the first edition is free and is perfectly good enough for us here.

If you want to just read the book and try out the examples, then fine, go ahead. Of course if you want to become a Lua expert you will need to read it all, but the main objectives here are to give you an idea of what a language like this can do and how it relates to your mathematics. For this, Chapters 1-5 are more than sufficient, and in fact we will not need anything like all of these five chapters either. (To my mind, Chapter 6 is where things start to get more difficult and technical, and those details here are definitely not needed. Of course many people will think that "more difficult and technical" = "more fun" and you are welcome to read as much as you like!)

The objectives in these web pages are not to repeat the book (you can read the book if you like, and it is very well written) but to highlight a small number of lua features that you can use on their own.

2. Basic commands

You can use lua like a calculator:

> print(2123+8765)
10888
> print("hello",23)
hello	23
> print(123*234)
28782
> print(289374/234)
1236.641025641
> print(8723648723*92837492834)
8.0988167580785e+20

Note that the last example depends on the version of lua you have, so if you don't get the same as me, just read on a bit further.

The main operations are + * - / for addition, multiplication, substraction and division. The "e+20" is the standard shorthand for " × 10 20 ". You can use this notation when you enter numbers too.

Corresponding to memories on a calculator, Lua has things called variables. (Variables are not at all like variables in maths. In particular you can change the value of variables, as we will see.) To save a value in a memory (or variable) you use the equals sign.

> a = 7654
> print(a)
7654
> print(a+a*2.2-2)
24490.8

Variable names can be a single letter (as here) or several letters or digits, possibly combined with the underscore _. The rule is that a variable name cannot have spaces or any other characters in it (!) and must not start with a digit, but the characters abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ are all allowed (and all considered distinct).

Expressions in lua are combined using the usual rules for precidence for example with * more tightly binding than + or -. Beware the difference between 2/3*4 and 2/3/4.

Exercise: how does lua compute 2/3*4 and 2/3/4? What are the precidence rules for * and / ?

The equals sign is a special command meaning "save this value in a variable". It does not mean anything at all similar to "equals" in mathematics. In particular you can write things like "a = a+1" which is not an equation to be solved but is rather an instruction, "get the current value of a, add one to it, and make the new value of a equal to the result."

> a = 7654
> print(a)
7654
> a = a+1
> print(a)
7655
> a = a*a
> print(a)
58599025

You can separate statements in lua with semicolons. These are not always needed, but if you are in doubt they are probably a good idea. And if you want to write text that will be ignored by lua (called comments) precede the text with --.

> a = 7654 ; b = 12378
> print(a*b)
94741212
> a = a+1 -- increment a
> b = b-1 -- decrement b
> print(a*b)
94745935

Comments are particularly useful when you are writing a longer script for lua in a separate file and you want to explain to yourself or some other human being how it is supposed to work.

There is a special syntax for comments that go over several lines. This is usually written as --[[ ... ]]. (Sometimes you will see this written as --[[ ... --]] That's just a trick to make sure the ]] gets ignored in case the opening --[[ was missing.)

Unlike a number of other languages, variables do not need to be declared in lua before you use them. (This is both a good and a bad feature.) If you use a variable before giving it a value it will appear as "nil".

> print(c)
nil
> c = 999
> print(c)
999

Exercise: What happens when you combine "nil" with numbers using + * - or / ?

3. Loops

We'll finish with something that computers are supposed to be good at, repetitive tasks.

for a=1,100 do
  print(a,a*a)
end

This is our first example of a loop. The special word "for" says do something for all possible integer values of a from 1 to 100 inclusive. In this case you should get a table of square numbers.

Try it.

Exercise: modify the above program to give a table of the first 100 triangle numbers 1,3,6,...

Question: what is the value of a when the loop has finished? Find out. Why do you think the Lua designers made Lua work this way?

Sometimes variables are "local" to a section of the program. The variable for a "for" loop is local in this way. However others are "global" i.e. accessible from all points in the program. If you don't say a variable is local and it isn't a control variable in a "for" loop then it will by default be global. The manual gives more information on this.

Challenge: if you introduce another variable or memory called "sum", perhaps one that is initially zero, you can modify the above program to calculate the sum of all integers from 1 to 100 inclusive.

Try to do this! If you have still got time spare go to the Lua pages and read the manual and try other things out.