lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


What is the most Lua-tic way of writing a function that:

(a) returns {m,m+k,...,m+(n-1)*k} given n?  I.e. please improve on:

    function arith(n,m,k)
        k = k or 1
        m = m or 1
        local s = {}
        for i=1,n do s[i]=m; m=m+k end
        return s
        end

(b) given a list of integers, returns the complement of that
    list in the range 1..n.  I.e. please improve on:

    function complement(x,n)
        local y={}
        for _,j in ipairs(x) do y[j]=true end
        local s = {}
        for i=1,n do if not y[i] then s[#s+1]=i end end
        return s
        end

Dirk