Herself’s Artificial Intelligence

Humans, meet your replacements.

Herself’s Artificial Intelligence header image 1

Lua scripting language

I’ve been hearing more and more about Lua and today finally had enough free time to download, install and play with it a bit.

I downloaded the source filse and they compiled and installed painlessly on the Mac, just follow the directions in the INSTALL file. There is a folder full of test applications. Open up a terminal and type:> lua hello.lua at a command prompt to be sure you’ve gotten everything up and running.

It is an odd looking language. Hello World looks like this:

– the first program in every language

io.write(”Hello world, from “,_VERSION,”!\n”)

Bisection method for solving de looks like this:
– bisection method for solving non-linear equations

delta=1e-6 – tolerance

function bisect(f,a,b,fa,fb)
local c=(a+b)/2
io.write(n,” c=”,c,” a=”,a,” b=”,b,”\n”)
if c==a or c==b or math.abs(a-b)
n=n+1
local fc=f(c)
if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
end

– find root of f in the inverval [a,b]. needs f(a)*f(b)<0
function solve(f,a,b)
n=0
local z,e=bisect(f,a,b,f(a),f(b))
io.write(string.format(”after %d steps, root is %.17g with error %.1e, f=%.1e\n”,n,z,e,f(z)))
end

– our function
function f(x)
return x*x*x-x-1
end

– find zero in [1,2]
solve(f,1,2)

( * These and several other example programs are included in the Lua download. )

You can keep your programs in text format and run them as scripts ( lua program.lua ) or you can compile them using the luac compiler into binary form. ( luac -o hello hello.lua )

Lua is dynamically typed, it can call and use C functions, it has it’s own threads so you can run routines concurrently * these are not OS threads.

The key word list for Lua is surprisingly small ( and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, while ) and it is a case sensitive language. All else is defined in lua_functions of which there are all the things you’d expect.

I found projects including Apache modules so you can use Lua for webscripts, Palm versions so you can write Palm programs with Lua, and more than a few games. Lua is used in game scripting and interactive AI scripting.

See also:
Cognitive Code develops software personal assistant using Lua

More information:
Lua Reference manuals
O’Reilly article, ‘Introducing Lua’
Introduction to Lua Programming
GameDev, ‘An Introduction to Lua’

Tags: topics in artificial intelligence

2 responses so far ↓

  • 1 unwesen // Nov 14, 2007 at 6:22 am

    So… I feel a need to comment here, but what I want to write may seem critical in a way I don’t mean it. The thing is, I don’t understand the hype Lua gets at the moment.

    It’s got it’s good points, but when people write about it, they quite often gloss over the bad points. It’s not terribly expressive for one thing, and extending it via C modules is a bit of a pain, just to name two.

    I’m all for people exploring new languages, it’ll always help their understanding of the benefits and downsides of languages they already know, and that’s a good thing. What always makes me a quit squirmy is when people take a look at one of the currently popular languages, because popular does not necessarily equal good (nor does it equal bad, to be fair).

    So if you’re curious about expanding your horizon, and assuming you don’t know that somewhat lesser known programming language, I’d suggest taking a look at Pike (http://pike.ida.liu.se/).

    Note that I don’t actually use Pike myself, I’m not promoting my preferred language here - I’ll stick to Python in just about any situation. But I found Pike very worthwhile to play around with; it’s extremely well documented, and manages to communicate the benefits of some of it’s design choices very well. Unfortunately, it also doesn’t get the attention it deserves.

  • 2 admin // Nov 14, 2007 at 8:49 am

    I agree that learning every new language has little value. But sometimes good things come in new languages. Structured programming, object orientated programming both showed up in new languages that were originally dished.

    Lua is getting attention because someone wrote what looks to be a cool project in it. Beyond that it is too early to say, I’ve only played with it a little bit.

You must log in to post a comment.