|
On 28 Jul 2006, at 13:24, Stefan Brantschen wrote:
Newbie question: I ran into the question how to use closures with recursion -- consider this snippet:local function visited (x) -- Check and register if a table 'x' has been visited local vis = {} return function (x) for _, v in pairs(vis) do if x == v then return true end v[#v+1] = x return false end
Other people have answered your closure problem I believe. I have a data structure improvement.
using vis as a characteristic table is way more efficient, simply associate "true" with each visited value:
if vis[x] then return true end vis[x] = true return false I hope I'm not barking up the wrong tree here. drj