Python may not be the best language for recursive approaches because (1) it doesn't support tail-recursion (2) function calls are really expensive (3) the recursion limit prevents deep recursions and (4) slicing sequences scales with O(n)
(where n
is the number of elements in the slice).
However you can use them for one-liners!
One approach would be just to successivly pop and add the first element until the sequence is exhausted:
>>> sum_func = lambda x: x[0] + sum_func(x[1:]) if x else 0
>>> sum_func(range(100))
4950
The first part is triggered as long as if x
(that is to say x
is not empty). This approach shows all the shortcomings of using recusion in Python: It hits the recursion limit for sequences of length ~300, it scales with O(n**2)
and it's really slow compared to the built-in sum
and reduce
approach.
One can mitigate one of the disadvantages by using a divide-and-conquer recursion approach:
>>> sum_func = lambda x: sum_func(x[:len(x)//2]) + sum_func(x[len(x)//2:]) if len(x) > 1 else x[0]
>>> sum_func(range(100))
4950
This time it recurses on both halfs of the list, thereby reducing the recusion depth from n
to log2(n)
, so it can handle even longer sequences. However it's not faster than the one above.
And of course there's always the cheat option:
>>> from numpy import sum
>>> sum(range(100))
4950
In case you really want it really fast :-)