List on recursive function

I need help with the following recursive function I’m trying to make.
This function is supposed to recursively iterate through a number of parts in an order and create a dictionary with each part as key, and the path to get there as its value, n times.
So for example:
Parts: 1, 2, 3, 4, 5; n = 3
Key: 1 -> Value: 1
Key: 2 -> Value: 1,2
Key: 3 -> Value 1,2,3

The key part I got all figured out, however I’m having trouble with the list of values.

Here’s the function:

local function calculateCapsuleCases(case, n, casesSoFar)
	local caseDictionary={} 
	local foundCase = workspace.Cases:FindFirstChild(case)
	table.insert(casesSoFar, case)
	if n > 0 then
		print("-=-Part 1-=-")
		print("Case: ".. case)
		for i = 1,#casesSoFar do
			print(casesSoFar[i])
		end
		print("-------")
		caseDictionary = calculateCapsuleCases(case + 1, n- 1, casesSoFar)
		print("-=-Part 2-=-")
		print("Case: ".. case)
		for i = 1,#casesSoFar do
			print(casesSoFar[i])
		end
		print("-------")
		if foundCase.Type.Value == "Blue" or foundCase.Type.Value == "Red" then
			caseDictionary[case] = casesSoFar
		end
	end
	return caseDictionary

end

Generated output:
-=-Part 1-=-
Case: 1
1

-=-Part 1-=-
Case: 2
1, 2

-=-Part 1-=-
Case: 3
1, 2, 3

-=-Part 2-=-
Case: 3
1, 2, 3

-=-Part 2-=-
Case: 2
1, 2, 3

-=-Part 2-=-
Case: 1
1, 2, 3

How come in Part 2, the list called casesSoFar has the same values “1,2,3” instead of holding their old ones from Part 1?

You’re looking for a node graph? What’s the use behind this

I just wanna know why casesSoFar is overwritten with those values

Not sure what you’re expecting… when you call that function, it restarts… why do you even need it to reiterate.

Here’s an example:
There are 4 numbers: you begin at 1, you continue by 2 and then there are two paths you can take, one leads to 3 and the other to 4.
Basically the output I expect is the following dictionary:
[“1”] = 1
[“2”] = 1, 2
[“3”] = 1, 2, 3
[“4”] = 1, 2 ,4
The key part I got nailed down. It’s the pathway to get there that is giving me trouble.
My idea was to insert the number case to the table casesSoFar each step of the recursion, so the first time the function calls with case = 1, casesSoFar = [1], then case = 2, casesSoFar = [1,2] and so on…
It starts well when I print the “Part 1” output, but then they all get the same values by “Part 2” and can’t figure out why.
Apologies if the explanation above was confusing :smiley:

You dont need a recursive function for this.

On the phone
Pseudo code:

Function(parts) {
 Cached = {}
 Tick = 0
 N = 0
 for (part in parts) {
      Tick += 1
     Cached[part] = {}
     While (Tick > N) {
         Table.insert(Cached[part}, N)
         N += 1 
         
      } 
     Table.sort(Cached[part], function())
 }
}

Obvsly not functional code but u get the gist.

This is not what I need. I’m only asking to know why casesSoFar prints all 3 numbers instead of doing it like in Part 1

Now you’re giving me an XY problem lol. Your problem is wanting to parse through a table of parts, cache the part with a value object with all the integers that represent each step.

I genuinely only wanna know where I went wrong in my recursion

You probably don’t have to think of what went wrong if you just questioned the way you wrote it.

So your table casesSoFar is the same table everywhere it is referenced so after it becomes 1,2,3 going forward the first time, it will always be 1,2,3 until you modify it again.

If you want the value to be the same for the duration of the function call (unique for each step/call), use a new table instead of the table passed in.

you can do

Example
local function recur(case, n, prevTable)
	local curTable = {unpack(prevTable)}
	table.insert(curTable,case)
	if n>0 then
		for i=1,#curTable do print(curTable[i]) end
		recur(case+1,n-1,curTable)
		for i=1,#curTable do print(curTable[i]) end
	end
end

recur(1,3,{})

P.S. Sorry, about the hostility, someone must be having a bad day. To his credit though, this is called passed by reference, meaning when you send a table somewhere (to another function) it is just pointing to the same exact memory location as the last table.

3 Likes

Thank you! This is what I wanted :slight_smile: