About my blog

I am multithreaded, and sometimes the threads get tangled.

tagline


a Studiolab production.

FIY

food design course at TUDelft
I blog in Italian
I have a LinkedIn profile
my lab is Studiolab!
I love Jasper van Kuijk's product usability weblog

the wind I am enjoying


Disclaimer

De meningen ge-uit door medewerkers en studenten van de TU Delft en de commentaren die zijn gegeven reflecteren niet perse de mening(en) van de TU Delft. De TU Delft is dan ook niet verantwoordelijk voor de inhoud van hetgeen op de TU Delft weblogs zichtbaar is. Wel vindt de TU Delft het belangrijk - en ook waarde toevoegend - dat medewerkers en studenten op deze, door de TU Delft gefaciliteerde, omgeving hun mening kunnen geven.

Posted in June 2010

Lua, a programming language that could be your new friend.

The programming language Lua (moon, in Portuguese) looks very interesting. It is small, contemporary and could be the complete solution for interactive prototyping, since it runs on single board computers, PC (Win, Mac, Linux) and servers. It can be a scripting language for Apache,  but it also hides inside Adobe Lightroom and vlc, the best videoplayer in the universe. In sort, it is interesting to people like me. If you find Lua interesting too, perhaps you are people like me, which means that we should never meet it or we risk boring each other to death.

At any rate, here is a small taste of Lua as a quick and dirty Fibonacci function:

function fib(i)
 if (i==1 or i==0)
  then return (i)
 else
  return fib(i-1) + fib(i-2)
 end
end

Of course, one would use a smarter form of recursion in order not to kill your computer as soon as you try printing fib(100). As you can tell from this brief snippet, Lua is dynamically typed and the end of a block is indicated by the keyword end, not by curly brackets (as in C) or fancy indentation (as in Python). The sprinkling of ends feels a bit Pascal-like.

The name of nothing

Amusingly, when you access an undefined variable you don’t get an error but the value nil. In the following code you find some educational language bits: text prefixed with a > sign was typed at the interactive interpreter.

 this very brief chat with Lua tells us that

  1. accessing an undefined variable produces a value of nil. Your program does not die instantly, but you do get a warning. This sort of implicit nil looks like a good thing, but it is probably a veritable quarry of trouble the moment you start playing around with it. And I kind of like the difference between a never-declared variable (perhaps because of a typo) and a variable that has been declared and happens to contain nil (or the local equivalent in the language at hand). But this is just me.
  2. this language does not care about whitespace.
  3. first-class functions, again typical of dynamic languages. You can also get them in Python and LISP. The moment you understand that a function is nothing magical but just another data type, you feel the light of understanding shine on your brow. No, C programmer: your pointers to functions are not first-class functions. Go back to scratching your silicon.
  4. the unary = operator is used for printing a value. Bizarre, but still within the limits of reason.
  5. even if I declared the function to take no arguments, I can call it with arguments and nothing happens.

 so many little details! Lua also has iterators, which is nice, and some really strange constructs for managing return values from function. Functions can return lists of values, which do not correspond to any other data type, which means that you need special syntax for converting them to reasonable value. For example, if foo returns a list of value, but you want only the first value returned by foo(4), you call (foo(4)). Or if you want to convert a list-of-return-variables into a table, you call {foo(4)}. Python too has not-so-wonderful syntax (I find it difficult to remember) around argument lists. 

Different, not equal

If you are a Pythonista or a C programmer, you are going to hate this. The symbol for "different" is ~= (tilde equal), not != (bang equal). Notice that if you fall into this very easy mistake, Lua outputs a useless error message. Be on the lookout.

Scope

Scope is where you go whoo, this is wacky. All variables declared inside a function are global. If you are a C programmer, your head just exploded, and what is left of your visual system is trying to turn that global into local. But no. So, supposing you have defined this trivial function:

function foo(x)
 z=1
 b=x*2
end

this is what happens in the interpreter:

> =z
variable ‘z’ is not declared
> foo(3)
> = z,b
1
6
> =x
variable ‘x’ is not declared

at least we don’t get x! The formal paramaters of a function do not count as variables declared inside it. There is still something sacred in the world. Notice that there is also a local keyword, and that local variable access goes faster than global variable access.

Nice stuff

We get coroutines. A classic computer science concept that can be used to write generators, a classic computer science concept that can be used to write better programs. 

We get metamethods, stored inside metatables (a word that looks funnier and funnier as you stare at it), and they are a bit like operator overloading. They can also be used to build some object oriented machinery, like classes and inheritiance. Lua also has lexical scoping, which will get all the computer science weenies excited since it is the road to closures.

Finally, we also get the bizarrely called function: if you call a function that does not exist on a table, lua looks for the function in the table’s metatable and calls it. In other words: we can build really reliable code that protects itself against programming mistakes. And we can really go crazy with metaprogramming. Examples here.

 

YouTube Preview Image

 

As you can see, there is a lot of emotion riding on programming languages, it is not just me.

 

PS: apologies for having to write with a tiny little picture, but I am not able to turn off the autoformatter, and lines and lines of underlined text might turn off even the most motivated reader.

© 2011 TU Delft