Re: Mutually recursive functions and local variables

  • From: "Soni L." <fakedme+lj@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 23 Jul 2015 14:01:13 -0300



On 23/07/15 05:42 AM, Geoff Leyland wrote:

Hi,

Is there a trick to writing a pair of mutually recursive functions such that
both functions end up being held in one-time-assigned local variables? The
following:

local two

local function one(count)
count = count - 1
if count > 0 then return two() end
end

two = function(count)
count = count - 1
if count > 0 then return one() end
end

means that (as I understand it) two cannot be proven constant, and so the code
generated is not as quick as it could be. (I haven’t actually done any
checking in this case, I’m just interested in whether there’s a simple answer).

Geoff
Use arguments.

local function one(two, count)
count = count - 1
if count > 0 then return two(one, count) end
end

local function two(one, count)
count = count - 1
if count > 0 then return one(two, count) end
end

local function realone = function(count)
return one(two, count)
end

local function realtwo = function(count)
return two(one, count)
end



Other related posts: