# Revision history [back]

### Type error in recursion

Consider the following function:

def f(n) :
def retfunc(x) :
return 1 if n == 0 else x*add(f(n-k) for k in (1..n))
return retfunc


The test

w = f(9); print w, type(w)


gives

"function retfunc at 0x43d3de8" "type 'function'"


which looks fine to me. However an evaluation w(3) gives

TypeError: unsupported operand type(s) for +: 'int' and 'function'


How can I get around this error?

### Type error in recursion

Consider the following function:

def f(n) :
def retfunc(x) :
return 1 if n == 0 else x*add(f(n-k) for k in (1..n))
return retfunc


The test

w = f(9); print w, type(w)


gives

"function retfunc at 0x43d3de8" "type 'function'"


which looks fine to me. However an evaluation w(3) gives

TypeError: unsupported operand type(s) for +: 'int' and 'function'


How can I get around this error?

EDIT: One solution is, as indicated by DSM,

def f(n):
retfunc(x) = 1 if n == 0 else add(x*f(n-k) for k in (1..n))
return retfunc


What I am trying to do here? Let's see:

for i in [1..8] : [c[0] for c in expand(f(i)(x)).coeffs()]
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]