0

I have a function:

def sum(list):
    if list == []:
        return 0
    return 1 + sum(list[1:])

And I couldn't find anywhere a normal explanation of how this part of the function works:

return 1 + sum(list[1:])

Why is 1 being added to the cut [1:] list, why is the cut [1:] list, what happens when 1 is added, and also why when I try to add 1 to the cut [1:] array outside of recursion, I get an error

Thanks in advance:)

  • In plain english, this is what the function is supposed to do: The sum of a list is zero if the list is empty. Otherwise, the sum of a list is the value of its first element plus the sum of the remaining elements. Can you see it? (Your code probably has a bug, `1` should be `list[0]`.) – timgeb Nov 29 '20 at 08:20
  • Your function just adds `1` for every item in the list. It's basically an inefficient version of `len(list)` I'm not exactly sure what you are asking. "and also why when I try to add 1 to the cut [1:] array outside of recursion, I get an error" Because, you can't add an `int` and a `list`, which is what `1 + list[1:]` tries to do. The recursive call to `sum` **returns an `int`**, and it's perfectly fine to add an `int` to an `int`. – juanpa.arrivillaga Nov 29 '20 at 08:29
  • @timgeb This function should count the number of elements in the list, and not sum them, and it somehow succeeds and I just want to know how?)) – Shit Coder Nov 29 '20 at 08:40
  • @ShitCoder I closed this as a duplicate because there is a canonical question about recursion. The example is very similar, and there are various good answers that might help in understanding / visualizing how the above works. – juanpa.arrivillaga Nov 29 '20 at 08:58
  • @juanpa.arrivillaga if not difficult, you could not provide links where this case is explained – Shit Coder Nov 29 '20 at 09:10
  • @ShitCoder sorry, what? There's a link at the top. – juanpa.arrivillaga Nov 29 '20 at 09:42
  • @ShitCoder Okay, if the function is supposed to compute the length of the list, then it is correct (although confusingly named, why is it called sum?). Here is the idea in plain english: The length of a list is zero if the list is empty. Otherwise, the length of the list is one plus the length of the same list with its first element removed. Clear? – timgeb Nov 29 '20 at 11:59
  • @timgeb But I have one question why the first element of the list is being removed, that is why spiks is list [1:] and not list [:] – Shit Coder Nov 29 '20 at 12:52
  • If you used `[:]`, then you would implement "The lenght of a list is one plus the lenght of the list" which gives you infinity. – timgeb Nov 29 '20 at 14:17
  • @timgeb Sorry, I understood correctly, when 1 is added, then the first element of the list is removed, then this continues until there are 0 elements left in the list and at the end those units that were added are added and the number of elements in the list is obtained, or how? I apologize for my stupidity) – Shit Coder Nov 29 '20 at 14:38
  • @timgeb If it's not difficult for you, you could not describe the full operation of the algorithm, for such a fool as me) – Shit Coder Nov 29 '20 at 14:44
  • @timgeb But why does [:] give an infinite loop? – Shit Coder Nov 29 '20 at 14:49
  • Because `list[:]` is the complete list. If you use `[:]` you define the length of a list as "The lenght of a list is one plus the lenght of the list" == "The lenght of a list is one plus one plus the lenght of the list" == "The lenght of a list is one plus one plus one plus the lenght of the list" = ... = ∞ – timgeb Nov 30 '20 at 17:21

0 Answers0