Demo Scripts

lua-users home

We want to [showcase] Lua. What scripts can do that? Add yours here...

-- Curried fripperies
put_in = function (t)
         local f
         f = function (k)
             if k then t[k] = true; return f
             else return end -- if
         end -- function
         return f
         end -- function
bag, enough = {}
put_in (bag) "wibble" (57) "grumpkin" "foo" (enough)
for k,v in pairs(bag) do print("bag has",k,"?",v) end

--- the classic recursive example:
function factorial(n)
    if n == 0 then
        return 1
        return n * factorial(n-1)

io.write("factorial of 10 is ", factorial(10), "\n")

-- and its tail recursive variant
fact = function (n)
       local f
       f = function (m,a)
           if m == 0 then return a end -- if
           return f(m-1,m*a) end -- function
       return f(n,1) end -- function
print("factorial of 10 is ",fact(10))

RecentChanges · preferences
edit · history
Last edited August 8, 2017 11:03 pm GMT (diff)