[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Recursion
- From: Yutaka Ueno <ueno@...>
- Date: Fri, 16 Jun 2000 11:54:49 +0900
Fred Bertsch wrote :
>
>If I use a function to recursively traverse a graph (or tree), it appears
>that I must make that function global. Is that true?
>
I think, we need to carry local variables in arguments of the
recursive function. Because the same kind of problem also happens when
DoSome() function wants to store results into a local variable,
which is also out of scope in the function traverse().
Then, we need another global again.
Following is another way using foreachWa(), we can carry additional
argument with function traverse().
This may looks still funny, but just an idea.
local traverse=function (index,subTree, a)
DoSome(subTree)
foreachWa(subTree,a,a)
end
end
foreachWa( tree, traverse,traverse)
function foreachWa(t,f,a) -- alternative to foreach()
local i,v=next(t,nil)
while i do
local res=f(i,v,a) -- just one more arg
if(res) then return res end
i,v=next(t,i)
end
end
Using pvalues always makes the code complex that is understood only by
experienced LUA programmer. So I would minimize use of those
magical staff and stay being simple.
-- ueno