Recursive functions not returning lists as expected
rickhg12hs
rickhg12hs at gmail.com
Tue May 4 01:45:16 EDT 2010
On May 4, 1:34 am, Cameron Simpson <c... at zip.com.au> wrote:
> On 03May2010 22:02, rickhg12hs <rickhg1... at gmail.com> wrote:
> | Would a kind soul explain something basic to a python noob?
> |
> | Why doesn't this function always return a list?
> |
> | def recur_trace(x,y):
> | print x,y
> | if not x:
> | return y
> | recur_trace(x[1:], y + [x[0]])
>
> You need:
> return recur_trace(x[1:], y + [x[0]])
>
> Otherwise the function returns None.
Ah, an explicit "return" is required. Thanks!
[To bad there's no tail recursion optimization.] 8-(
More information about the Python-list
mailing list