 The following practical will give you an overview on the Lua scripting language. Lua pronounced Lua means moon in Portuguese and it is a powerful, efficient, lightweight, and valuable scripting language which allows you to program with different paradigms like procedural programming, object oriented programming, and functional programming. The tutorial is divided in the following chapters. We start to get acquainted with the terminal, how to open it. We'll see the torch ripple, read, evaluate, print, loop. To have an interactive session with the Lua scripting language, we'll learn how to write comments, how to use numbers, operators, and mathematical functions on these numbers, how to write strings. We will learn about nil and the who function, the while and for loops, and the tables. Moreover, we'll see how to write functions, how to quit our ripple, and at the end, how to write modules and local variables in scripting files. To open a terminal on OSX, we can press Command and Space to open the spotlight search. Then we can type terminal and press Enter to open the terminal. On a Ubuntu machine, we can press Control, Alt, and T. The terminal will open. To have the same settings I'm using on this machine, you can go on this website and follow the instructions. If you're using the virtual machine we provided for the class, all the settings are already on your machine. To learn some Lua syntax, we'll be using the torch ripple, which is a read, evaluate, print, loop, interactive, top-level interface. The tutorial can be found at this location. To open torch ripple, we can simply type th. To type a comment, I can simply type double dash, so this is a comment. And press Enter. By pressing Control L, we can clear the screen. To insert a multiline comment, we can type dash dash, open square brackets, type our multiline comment, and then we can close it in a similar way with dash dash, close, close, and press Enter. We are going to be talking now about variables and numbers. All numbers in Lua are 64-bit doubles. So basically, like real, we don't have integers, we don't have float, we don't have other types. So, for example, number equal 42. And if I press Number and press Enter, we have 42. We have the five standard operations on numbers, summation, subtractions, multiplication, division, and the reminder for the integer division. And we have also a mathematical library we can access by typing math. Pressing twice the tab character, we have a list of more advanced functions. For example, we can compute the absolute value of 5 minus 7 on expression, which is evaluate into minus 2. And then we have the absolute values of minus 2, which is plus 2. So strings. For example, we can have s equal tc is a string. Or we can have, for example, t equal double quotation, double quote string, which is exactly the same of the previous one. Or one more way to write a string is going to be using the square bracket operator. So we can write square bracket, square bracket, multiline string definition. And then we can print them by just typing s enter, or t enter, or u enter. And we notice that we have inserted a empty line at the end because the last couple of square brackets were on the new line below. We can undefine variables by assigning to them the nil value. We can kill the last string, for example, by typing u equal nil. If we check now the variables that we have defined with the function who, we can see that we have just t, num, s, and result, which is the output of the last command. Control L once more to clean the screen. Oops. So we can type while, and then we have a condition which has to be evaluated. For example, num below, lower than 50. Do. Now we can have, we can type the body of the while loop, for example, num equal num plus one. And then type n. And then we can verify that num, now it's 50. So we sum one to num from 46, that was its previous value, up to the point in which it is 49. And we still sum one, so we end up to 50. In a similar way, we can also write a for loop. For example, for i equal one to five, do. And then we can do io dot write. We can write i dot dot space. Then we do the output are going to be the numbers from one to five. In a similar way, we can also write a for loop with a one more extra parameter, which allow us to write, for example, the numbers in reverse order, for example, for i that equal five to one, and the increment is going to be minus one. Do again io write, i concatenate a space, and then end. And then we have the reverse sequence. Another common way to use for loop is with arrays. We can type four index and value in i pairs of the following array. So a, b, c, d, do, print index and value. And so we have that the indexes of these list of values are one, two, three, and four. And the corresponding values are a, b, c, and string d. In a similar way, we can do a for loop over a dictionary or a generic table. And it's very similar. So it's going to be so for key and the value in pairs. So there is no i in this case. And then here we have our dictionary a equal three, b equal five, c equal eight, and d equal two, do, print the key and the value. And so it prints all the keys a, b, d, and c with their corresponding values three, five, two, and eight. But it's important to notice there is no order. So in a table, there is no order. Whereas in array, which is a table in which the key are corresponding to integer starting from one up to the length of the, of the table, we'll have, we'll maintain order when used with the IPRs function. Therefore, we have seen that there are two types of tables. There is the list or array version. And there is the dictionary. So let's start with the first one. We have table one equal. I can write here a list of values, for example, a string, a number, a Boolean, true. I can write again, number, and one more string. And then I can see what t1 contains by typing t1 and press enter. We can access different values of a table, which is a list, by typing t1. And then I put a number, for example, number four. And then I access the fourth element, which is equal to three. And I can even type t1 of four. And I can assign a new value, for example, false. And then if I print t1, we can see that the fourth value has been changed from three to false. The dictionaries that are the most generic table we can think of. To define a dictionary, it is very similar to the way we define a list table. So we have to specify the key in this case. So we can call, for example, key abc. And we can have equal to four. Then key def equal nine. Key three, number three, equal to a string. And then we can close this one. So if I type t2, now we have that to access these different values, I can type t2.tabtab. And I have just the string version of the key. So I can do abc, press enter. And we have the number four, or t2.tabtab def, enter, number nine. But to access numeric positions, I had to use a square bracket operator, number three. In the same way, I could also access the string operators by the square bracket and abc. So let's see how we can append and add new elements to lists and dictionaries. So let's start with t1. We can do table, tab tab, insert to table one, the element eight. And then if I print t1, we will have the new element in position six containing the value eight. To know the size of a table, we can use the hash operator. t1 with the hash in front will give us the length of our list. So for adding a new element to a list, we can do t1. And we say size of t1 plus one. And then we put our new value. And then we have t1. If we do type t1 of number nine, and we assign a value, this one, t1 is no longer a list, but becomes a generic table. And iPairs won't work on a sequence of numbers that are not contiguous. For dictionaries, so let's take out t2. We can define new new items, simply putting a dot, the name of the new key. And then we can assign a new value. And then if I do t2, we can see here it's assigned a new value. Let's use now functions to determine how to see how Lua works with conditional statements. So I can define my function. Let's call it f, which takes a input parameter x, and does the following. So if x then print true, and then x itself, else print false, and still x itself. So we can see what it is equal to. We close the if, and then we close the function. So, of course, if we send to f true, it's going to evaluate true and it's true. And then if we send false, it's going to evaluate as false, and then it's false. Now, interestingly, we can also try to send values that are not Boolean. So everything that is not nil, which is going to be evaluated as false, is considered true. So f of a numeric value, it's true. f of a string, it's also considered true. And f of a table, let's put a simple list. It's also considered true. We can exploit this language feature to implement default values for function parameters. Let's create a new function, function g of x, and which is going to be simply printing x if it's defined, or we just predefined a value for it. So x equal x, or let's put the string undefined. Here what happens is that if x is a value, and so if it's anything that is not nil or false, then x is going to be x itself. If instead we send a nil or a false value, the or will simply return the second argument. So if I do here print x, and we can see if I call g with a value, it's going to print the value, even a string. Or if I send nothing, means I send a nil inside, which is the same, or I send a false, but that's not how it's used usually. It's going to be taking the value we have just predefined here. Similarly, we can define inline if statements. So let's create one more function, function h of x. And in this case, I would like just to print x. So if x is greater than zero, and we're going to output plus, or we are going to put a minus. So the function between the parentheses says if x is greater than zero, then the first one is going to be the output of our evaluation of statement. Otherwise, if this is false, with the n is also false. And as we saw before, the or would be outputting the second part. So let's try end. So h of three is going to be a plus. And h of minus nine, it's a minus. Functions can also return functions. For example, write our function adder x, which returns another function. So functions are first citizens in Lua, which doesn't have a name. And we call on the parameter y return x plus y and and then we end the other function. So if we call adder with a value of five, a is a function still. So we can call a with number two. And this one is going to be evaluating to number seven. Okay, so now we see the last part, which is quite important. And there are the modules how to play with external files. So we can quit the triple by typing control D. And then yes, why for yes, we'll use T max in order to split the windows in two parts. Let's CD into the code, which is directory where I will put my files. And let's start with my module. So I call this module mod dot one. This is my first demo module. So I start with local m equal the empty table. Local it's necessary so that I will not be polluting the whole code with my variables, but I will keep them private to this file. So let's define the first helper function. Say my name, which is simply printing my name, and then a function which is going to be inside the table above. So m dot say, hello, which print, hello, there. And then say my name. And then we have the function, the module returns the table m. And that's it. So I can save. I split my windows call it control a and quotation. So control a plus quotations. And here we can call our record torch control L for cleaning the screen. And mod equal require mod. And then if I print mod, we see it's a table with a function, say hello. So control a k to go up. So say my name. It's a local function, which is local to only the mod dot Lua file. And therefore, it does not appear below. Control a j to go below. We can try to call say hello, mod dot say hello. And then, yes, it's going to print out what we expect. If I try to call the other function, mod say my name, we will get a attempt to call field, say my name, a little value, because it's undefined because it's local to the mod file. Let's go above control a k. Let's go at the end. Sorry. So here before returning, let's put also a print all code. And then we save. We copy low control a j. So we can do require mod as we did before. And well, nothing happened. We don't have the print all code executed anywhere. This is because low require does cache the files. So once it's it's run once, it won't run again, the code. If you'd like to run the new code, we already we just saved, we had to use do file. And then we had to put the name of the file. Again, here we can call mod equal. And then all code loaded. Cool. If I do again the same, perhaps I change your mode to still all code loaded. But if I do mod three equal require mod, it won't print all code loaded. And actually mod three, it's the previous mod we have run the first time. If we'd like to run to load the code, but do not run at the same moment, we can do in this way, we can do f for file equal load file. And then I can say, it's like the do file, but it doesn't do it. It just loaded some main memory. So mod dot lower. And so f it's a function. And if I run the function, then it's actually executing the code. The last the last interesting load file load string load command is this one. It's called load string. And here we can write a command. So 123 and load string. So G is a function. If I call G, G is going to be evaluating the string we send to G. As a general reminder of good practice, always use local variables in your script, unless you are in the trouble and you are coding with the interactive interface, which does not allow you to properly use local variables. Local variables are mandatory in order to do not pollute the scope of the whole program.