lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index] <> wrote:
> I have the following code:
> But Lua says
> error: cannot access a variable in outer scope;
>   last token read: `f' at line 7 in file `a.lua'

You need to use "upvalues" (see the manual), e.g.

add = function (x, y)
        return x + y

partial = function (f, i)
        local c = function (j)
                        return %f(%i, j)
        return c

add_one = partial(add, 1)

print (add_one(2))

CINEMUCK (SI ne muk), n.  The combination of popcorn, soda, and melted
chocolate which covers the floors of movie theaters.
	-- Rich Hall, "Sniglets"