I have the following function:
def lst(*l):
if l==():return None
else: return cons(l[0],lst(l[1:]))
When I run it, I get "maximum recursion depth exceeded in comparison". Curiously, when I add a warper which converts the parameter tuple into a list, everything works just fine:
def lst(*l):
return _lst(list(l))
def _lst(l):
if l==[]:return None
else: return (l[0],_lst(l[1:]))
>>> lst(1,2)
(1, (2, None))
What is the problem and how to deal with this strange behavior?
if l==():return Nonewithif not l: return None. Also, what doesconsdo? You might be able to replace this withreduce.consis a constructor like that:def __init__(self,car,cdr):self.car=car;self.cdr=cdr. I tried to replace the recursion withreduce, but something went wrong (seems likereduceworks "wrong" direction) and I have absolutely no idea how to get it the right way. As long as i have a working code, thanks to Samy Arous, I wish not to make it broken. Thank you anyway.