def mysum(L):
print(L)
if not L:
return 0
else:
return L[0] + mysum(L[1:])
mysum([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
15
def mysum1(L):
return L[0] if len(L)==1 else L[0] + mysum(L[1:])
mysum1([1, 2, 3, 4, 5])
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
15
def mysum2(X):
first, *rest = X
return first if not rest else first + mysum(rest)
mysum2([1, 2, 3, 4, 99])
[2, 3, 4, 99]
[3, 4, 99]
[4, 99]
[99]
[]
109
def mysum3(Y):
if not Y: return 0
return noempty(Y)
def noempty(Y):
return Y[0] + mysum3(Y[1:])
mysum3([5, 2, 3, 4, 99])
113