lua-users home
lua-l archive

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


-- Some typo in the previous version

local function infix(...)
   local function eval(t)
      local l = t[1]
      if type(l) == "table" then l = eval(l) end
      for k = 2,#t,2 do
	 local f, r = t[k], t[k+1]
	 if type(r) == "table" then r = eval(r) end
	 l = f(l, r)
      end
      return l
   end
   return eval(...)
end

local function add(a,b)
   return a + b
end

local function sub(a,b)
   return a - b
end

local function mul(a,b)
   return a * b
end

local function div(a,b)
   return a / b
end

print(
   infix{
      12 ,add, 3 ,add, 10 ,sub, { 30 ,mul, 10 } ,div, { 5 ,add, 5 } } )

-- Should print: -27.5