Lua: Day 1

The Call to Adventure

So, it’s good I played a little with Lua as I was installing it, because the first day’s material is just not that inspiring. The big problem presented is that CSV files have some problems. Specifically, there’s no way in CSV to indicate constraints or collections. Okay, fine. That’s not really what CSV is all about. But apparently this should be enough to motivate me to keep going. Meh. It’s good I was motivated to begin with. (Like, what about XML?)

The description of Lua as a table-based language that can easily adjust to different paradigms holds lots of promise. That one of these paradigms can be the prototype paradigm is even more interesting. In Seven Languages, the only prototype language was Io and it’s fallen from popularity. When I last taught it, students poked around and realized there had been no activity on the Io community for months. (JavaScript is also a prototype language, but there’s a lot of other baggage with JavaScript.) The book doesn’t get to tables until the second day though, so enough said, for now.

From the first day, the most I can say is that Lua is a fairly typical scripting language so far. The REPL (Read-Eval-Print Loop) is a bit unusual in that you can’t just type:

2015

and get the result

2015

Instead, you need to explicitly indicate you want Lua to evaluate an expression, as in:

=2015

or you need to indicate you want that value to be returned, as in:

return 2015

I’ve also been doing a lot of Python recently, so the fact that Lua is not at all line-based is a nice change. You want to enter two commands over three lines like:

print
"Hello" print
" world"

Yeah, sure, go for it.

Lua is implemented using a “strict subset of ANSI C” (umm, quotes, yeah, they come from “Seven More Languages in Seven Weeks” unless otherwise indicated). It just uses floating point for all numbers, but does allow for the % operator. And it does something sorta reasonable when you use % and floating point numbers. 13.2 % 4 is 1.2 and 14 % 3.3 is 0.8 . (Let’s not think about negative numbers now, okay?)

There’s also exponentiation and it uses the correct precedence: 2 ^ 3 ^ 2 is 512, not 64. Expressions are otherwise unremarkable.

Booleans have the literals true and false and use real words instead of symbols (and, or, not). As I fussed about before, not equals is ~= . Only numbers and strings can be compared (and strings can only be compared to strings, numbers to numbers, not of this 42 < “43” stuff.) Strings can use single or double quotes. Use # before a string (or variable containing a string) to get its length. Use .. for concatenation. (And no, you can’t get the length of a number; I tried.)

Lua’s quite laid back about uninitialized variables and parameters. Try to print a variable that has no value and you get nil. Of course, it’s pickier if you try to use uninitialized variables in expressions. Combining this with multiple assignment leads to some interesting results:

x, y, z = 1, 2, 3

behaves as expected (if you expect x to get 1, y to get 2, and z to get 3). And

x, y, z = 1, 2

doesn’t complain, just sets z to nil (even if it had a value before). Finally,

x, y, z = 1, 2, 3, 4

just ignores that pesky extra 4 at the end.

Control structures are also fairly routine. They’re ended with end, so there’s no problem with dangling elses. The for loop makes the Pascal programmer in me happy:

for i = 1, 5 do
   print("i = " ..i)
end

And the loop control variable behaves nicely–it is reset to the “correct” value at the beginning of each loop. You can change it, but it gets changed back:

for i = 1, 5 do
   print("Before increment, i = " ..i)
   i = i + 1
   print("After increment, i = " ..i)
end

results in:

Before increment, i = 1
After increment, i = 2
Before increment, i = 2
After increment, i = 3
Before increment, i = 3
After increment, i = 4
Before increment, i = 4
After increment, i = 5
Before increment, i = 5
After increment, i = 6

Yay!

And i is nil after the loop. (Since I want to practice more with GitHub, I’ll be putting programs in the repository https://github.com/Annie29/7MoreLanguages . This is the first one out there.)

There’s also while and repeat loops (yes, repeat, like Wirth and God intended it, none of this do silliness!).

The author tosses off what I think is an important feature: Lua optimizes tail recursion. Now, tail recursion optimization was covered in detail in Seven Languages in Prolog, Clojure, Erlang, and Haskell, so maybe it’s just not that exciting. Maybe I’m prejudiced since I remember how hard it was for students to understand the benefits of tail recursion removal. But since I want to play with functions more before going on, especially since they’re first-class values and I’ve only recently grokked closure, and this has gotten long enough, so let’s hold that off until the next posting.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: