a little bit of recursion

D

Dominik Kaspar

I'm trying to write a quite big recursive function, but the baseline
is the following:

def rek(N, L):
N = N + 1
if N >= 9: return L
else: return rek(N, L = L.append(N))

print rek(0, [])

Why isn't this working?
In my opinion it should return [1, 2, ..., 9]

Thanks for any answers.
Dominik
 
R

rzed

Dominik said:
I'm trying to write a quite big recursive function, but the baseline
is the following:

def rek(N, L):
N = N + 1
if N >= 9: return L
else: return rek(N, L = L.append(N))

print rek(0, [])

Why isn't this working?
In my opinion it should return [1, 2, ..., 9]

Thanks for any answers.


One problem is that L = L.append(N) does not have the effect you want.
Try it on its own.
Another is that it won't return [1,2,...,9] even when that is
corrected.
 
S

Skip Montanaro

Dominik> def rek(N, L):
Dominik> N = N + 1
Dominik> if N >= 9: return L
Dominik> else: return rek(N, L = L.append(N))

In the recursive calls, L is None. Just pass L as the second arg:

def rek(N, L):
N = N + 1
if N >= 9:
return L
L.append(N)
return rek(N, L)

Skip
 
G

Gregor Lingl

rzed said:
Dominik said:
I'm trying to write a quite big recursive function, but the baseline
is the following:

def rek(N, L):
N = N + 1
if N >= 9: return L
else: return rek(N, L = L.append(N))

print rek(0, [])

Why isn't this working?
In my opinion it should return [1, 2, ..., 9]

Thanks for any answers.



One problem is that L = L.append(N) does not have the effect you want.
Try it on its own.
Another is that it won't return [1,2,...,9] even when that is
corrected.

That certainly depends on the way one corrects it.
However Dominik first has to notice, that append is a
method, which *changes* L but doesn't return L. Instead
it returns None. So it may be ok to call L.append(N),
but not on the right side of an assignment statement.

You can obsrve what I mean here:
>>> L = [1,2,3]
>>> print L.append(4) None
>>> L [1, 2, 3, 4]
>>>

HTH, Gregor
 
G

Gregor Lingl

Skip said:
Dominik> def rek(N, L):
Dominik> N = N + 1
Dominik> if N >= 9: return L
Dominik> else: return rek(N, L = L.append(N))

In the recursive calls, L is None. Just pass L as the second arg:

def rek(N, L):
N = N + 1
if N >= 9:
return L
L.append(N)
return rek(N, L)

Skip
Nice, but will not append 9 as desired
Gregor
 
G

Gregor Lingl

Skip said:
Dominik> def rek(N, L):
Dominik> N = N + 1
Dominik> if N >= 9: return L
Dominik> else: return rek(N, L = L.append(N))

In the recursive calls, L is None. Just pass L as the second arg:

def rek(N, L):
N = N + 1
if N >= 9:
return L
L.append(N)
return rek(N, L)

Skip
Nice, but will not append 9 as desired
Gregor
 
S

Skip Montanaro

Gregor> Skip Montanaro schrieb:
Dominik> def rek(N, L):
Dominik> N = N + 1
Dominik> if N >= 9: return L
Dominik> else: return rek(N, L = L.append(N))

Gregor> Nice, but will not append 9 as desired

Sure, but that's a logic error, not a misunderstanding about what L.append()
returns. I suspect the latter was what confused Dominik. Once he runs
something that doesn't raise an AttributeError, he'll quickly discover that
his test should be "if N > 9". He couldn't even get to that point with his
original code.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top