Newbi Q: Recursively reverse lists but NOT strings?
Dmitri O.Kondratiev
dokondr at gmail.com
Sun Oct 14 17:06:19 EDT 2007
The function I wrote (below) reverses lists all right:
def reverse(xs):
if xs == []:
return []
else:
return (reverse (xs[1:])) + [xs[0]]
>>> reverse ([1,2,3])
[3, 2, 1]
>>>
Yet when I try to reverse a string I get:
>>> reverse ("abc")
...
...
...
File "C:\wks\python-wks\reverse.py", line 5, in reverse
return (reverse (xs[1:])) + [xs[0]]
File "C:\wks\python-wks\reverse.py", line 5, in reverse
return (reverse (xs[1:])) + [xs[0]]
File "C:\wks\python-wks\reverse.py", line 2, in reverse
if xs == []:
RuntimeError: maximum recursion depth exceeded in cmp
>>>
What's wrong? Why recursion never stops?
Thanks,
Dima
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071015/6f989723/attachment.html>
More information about the Python-list
mailing list