def remove_all1(x,s):
def loop(x,s,ss):
while s!=[]:
if x == s[0]:
return loop(x,s[2:],ss+[s[1]])
else:
return loop(x,s[1:],ss+[s[0]])
return ss
return loop(x,s,[])
This is the one I made by recursive tail function to remove recursive value x and if :
print(remove_all1(3,[4,3,5,6,3,2,1]))
I would get:
[4,5,6,2,1]
The exact result I want. But for the recursive function here:
def remove_all0(x,s):
while s!=[]:
if x == s[0]:
ss = [s[1]] + remove_all0(x,s[2:])
return ss
else:
s1 = [s[0]] + [remove_all0(x, s[1:])]
return s1
if s==[]:
return s
print(remove_all0(3,[4,3,5,6,3,2,1]))
I would not get the same result. Instead, I got:
[4, [5, 6, [2, 1, []]]]
Why does this happen?