This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jerimo's profile picture

Recursive Function Problems

Started by jerimo, 10 March 2015 - 03:25 AM
jerimo #1
Posted 10 March 2015 - 04:25 AM
Hi, I'm working on a program, where I need it to recursivelly call itself at times; the problem is that by calling it all the variables get reset and dont work anymore, coming from other languages this stumped me.

So, how could I go about using a loop counter, calling the function from inside itself and upon returning have the values set back to where they should be?
Lyqyd #2
Posted 10 March 2015 - 04:47 AM
Please post your current code.
Bluemanduck #3
Posted 10 March 2015 - 07:12 AM
you can initialize variables outside of the function. I imagine your code looks something like this:


local function foo()
   a =1
   b = 2
   do something to a and b
   recursive call
end

But if you make the variables global, they will retain their states and not get reset. Like so:


local	a =1
local	b = 2
local function foo()
   do something to a and b
   recursive call
end

Of course, I could be totally wrong. Code would be nice to see, like Lyqyd said.
Edited on 10 March 2015 - 06:20 AM
jerimo #4
Posted 10 March 2015 - 07:46 AM
Basically this

function runProgram(program, loops)
        if program[1] == "set" then
                table.remove(program, 1)
                program["NAME"] = program[1]
                table.remove(program, 1)

        elseif program["NAME"] then

        else
                program["NAME"] = "MAIN"
        end

print("\nExecuting: ", program["NAME"], " of length ", #program, ", ", loops, " times")
        while loops > 0 do
                loops = loops - 1

                program["COUNT"] = 0
                depth = 0
                while program["COUNT"] < #program do
                        program["COUNT"] = program["COUNT"] + 1
print(" ", program["NAME"], " >[count] ", program["COUNT"], " ( ", program[program["COUNT"]], " )")
                        if type(program[program["COUNT"]]) == "table" then
                                if tonumber(program[program["COUNT"] + 1]) then
                                        runProgram(program[program["COUNT"]], tonumber(program[program["COUNT"] + 1]))
                                        program["COUNT"] = program["COUNT"] + 1
                                else
                                        runProgram(program[program["COUNT"]], 1)
                                end

                        elseif program[program["COUNT"]] == "(" then
                                depth = depth + 1
                                program[program["COUNT"]] = {}

                                if program[program["COUNT"] + 1] == "set" then
                                        table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"]+1))
                                        program[program["COUNT"]]["NAME"] = program[program["COUNT"] + 1] .. program["NAME"]
                                        table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))

                                else
                                        table.insert(program[program["COUNT"]], "set")
                                        table.insert(program[program["COUNT"]], program["NAME"] .. " >] " .. tostring(program["COUNT"]))
                                end

--print("\nNew SubProgram: ", program[program["COUNT"]]["NAME"])
--print("DEPTH: ", depth)

                                while depth ~= 0 and program["COUNT"] < #program do
                                        if program[program["COUNT"] + 1] == "(" then
                                                depth = depth + 1
                                                table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
--print("DEPTH: ", depth)

                                        elseif program[program["COUNT"] + 1] == ")" then
                                                depth = depth - 1
--print("DEPTH: ", depth)
                                                if depth == 0 then
                                                        table.remove(program, program["COUNT"] + 1)

                                                else
                                                        table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
                                                end

                                        else
                                                table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
                                        end
                                end

                                if tonumber(program[program["COUNT"] + 1]) then
                                        program[program["COUNT"]] = runProgram(program[program["COUNT"]], tonumber(program[program["COUNT"] + 1]))
                                        program["COUNT"] = program["COUNT"] + 1
                                else
                                        program[program["COUNT"]] = runProgram(program[program["COUNT"]], 1)
                                end

                        elseif program[program["COUNT"]] == ")" then
                                print("\n\nFound \")\" when not in a sub-program, maybe missing a space at \"(\"?")
                                break

                        elseif APIList["API"][program[program["COUNT"]]] then
                                if tonumber(program[program["COUNT"] + 1]) then
                                        program["COUNT"] = program["COUNT"] + 1
print("    ", program["NAME"], " [-", program[program["COUNT"]], "-> ", APIList["NAME"], ".", program[program["COUNT"]-1], "()")
                                        for i=1,tonumber(program[program["COUNT"]]) do
                                                APIList["API"][program[program["COUNT"]-1]]()
                                        end

                                else
print(program["NAME"], " [-> ", APIList["NAME"], ".", program[program["COUNT"]], "()")
                                        APIList["API"][program[program["COUNT"]]]()
                                end

                        elseif tonumber(program[program["COUNT"]]) then
print("\tNumber: ", program[program["COUNT"]])

                        else
                                os.run( {}, tostring(program[program["COUNT"]]))
                        end
--os.pullEvent( "key" )
                end
        end

        program["COUNT"] = nil
        return program

end
I found a way to make it work, by using the fact that it's a table to my advantage, but was honestly not expecting the variables to do such a thing.

you can initialize variables outside of the function. I imagine your code looks something like this:


local function foo()
   a =1
   b = 2
   do something to a and b
   recursive call
end

But if you make the variables global, they will retain their states and not get reset. Like so:


local	a =1
local	b = 2
local function foo()
   do something to a and b
   recursive call
end

Of course, I could be totally wrong. Code would be nice to see, like Lyqyd said.
You are not wrong, but the problem was that I needed a fresh set of the variables each call, that would then return to their old value after the call, like c++ and such, but found a way to make it work, as mentioned, thanks anyways!
Bluemanduck #5
Posted 10 March 2015 - 08:43 AM
Hah! I vastly underestimated what you were talking about. Glad you fixed it, that looks like a hot mess.
jerimo #6
Posted 11 March 2015 - 05:34 AM
Hah! I vastly underestimated what you were talking about. Glad you fixed it, that looks like a hot mess.
Heh yup, planning on making it a full on language if time permits, quite a project for my first delve into lua, but quite fun!