Hey, I'm trying to implement the merge function from merge sort in Lua. I know the algorithm pretty well, but I'm new to Lua. I keep getting a "bad argument #1 to 'insert' (table expected, got nil)" I believe the error is pointing at my recursive call. I can't figure it out and I have a feeling it's something pretty trivial. I just need a Lua guru to give me some guidance. Thanks. Here's my function:
function merge(l1, l2)
if # l1 == 0 then
return l2
elseif # l2 == 0 then
return l1
else
if l1[1] <= l2[1] then
tmp = l1[1]
table.remove(l1,1)
return table.insert(merge(l1,l2),tmp)
else
tmp = l2[1]
table.remove(l2,1)
return table.insert(merge(l1,l2),tmp)
end
end
end