5

Despite having started learning programming with Pascal and C, after the jump to OO (C++, Java) I lost sense of the structured programming paradigm. I have started learning Lua and I have researched many tutorials, but all of them only cover basic operations and language features and capabilities. They feel more like a reference doc than a programmer's guide.

Now, when trying to work with day to day tasks, how does one go through most common design patterns like observer, or multithreaded programming, creating UI elements and polling system calls for keyboard or sensors? Is it even feasible in this languages or you have to work with the C binding, libraries and low-level programming to get most stuff done? Do I get the Lua scope wrong?

2 Answers 2

8

Besides the reference doc (which is a good way to learn a small language IMHO), there's Programming in Lua (PiL). It teaches both the language and the common idiomatic patterns.

About scope, Lua is a high level language. It allows you to quickly express algorithms and create structures. It's also widely used as an extension language, to be embedded within an application.

There are many C modules available, both to add new capabilities (LPEG, LuaSocket, etc.) and to 'bind' to existing C libraries (databases, XML parsers, etc.), but Lua itself is a complete language, you're not required to write C modules on day-to-day work.

About the "most common design patterns" do you mean the "Gang of Four" design patterns? (you mention the Observer pattern). If so, While really useful and written in a language-agnostic tone, many of these patterns make sense only in statically-typed, heavily OO languages like C++, Java, C# and a few others. With other platforms, some are unnecessary (like the Command Patterns, which becomes a closure when you have lexically scoped, first class functions), some others are applicable but result in widely different solutions.

About multithreaded programming: Lua itself is mostly single-threaded. There are several ways around this, but they're not so heavily needed as on other platforms. A simple coroutine-based scheme is quick and easy to write and works perfectly in many cases. When not, the exact way to do multithreading depends a lot on your needs.

GUI: this is in the realm of a GUI library and not of the language. There are several available. Again, you can pick any one, or none at all. There are many things to do without GUIs (command line, application extensions, servers, etc)

Hardware integration: depends on the hardware. In most cases, there should be a C API and so you need a binding library, which is not hard to write (sometimes it's just a 5-20 line C function for each API entry point)

1
  • 2
    +1: "many of these patterns make sense only in statically-typed, heavily OO, languages; like C++, Java, C#" Nov 14, 2012 at 21:02
8

Lua is, in many ways, very similar to Scheme: procedural with first-class and higher-order procedures, block-structured, lexically scoped with lexical closures, proper tail calls, use of a single pervasive data structure (list in Scheme, table in Lua) throughout the entire language.

The Scheme community has produced some extremely high quality programming books and tutorials, which are all more or less directly applicable to Lua.

and many many many others.

One important difference between the two languages is that even though both are impure imperative languages, the Scheme community actually prefers a very purely functional, immutable, side-effect-free, declarative programming style than the Lua community. So, if you learn Lua via Scheme, your code might not be fully idiomatic Lua. I don't think that's a bad thing: first of all, functional programming is a good thing, and secondly, you already know imperative programming from your forays into Pascal, C, C++ and Java anyway.

1
  • Despite liking your answer, Javier's one is more accurate answering my doubts. Hope you don't mind I accepted his :) Nov 15, 2012 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.