On 2007-09-13, Ian Clark <[EMAIL PROTECTED]> wrote:
> Neil Cerutti wrote:
>> On 2007-09-13, Gigs_ <[EMAIL PROTECTED]> wrote:
>>> Can someone explain me this
>>>
>>>>>> def f(l):
>>>     if l == []:
>>>             return []
>>>     else:
>>>             return f(l[1:]) + l[:1]  # <= cant figure this, how is all sum 
>>> at the end?
>> 
>> In plain English, the above program says:
>> 
>> The sum of the items in list l is zero if the list is empty.
>> Otherwise, the sum is the value of the first item plus the sum of
>> the rest of the items in the list.
>
> Am I missing something? What does this have to do with summing?
>
>      >>> def f(l):
>      ...     if l == []:
>      ...         return []
>      ...     else:
>      ...         return f(l[1:]) + l[:1]
>      ...
>      >>> f([1, 2, 3, 4])
>      [4, 3, 2, 1]

It says: You need to read more than the first sentence of a
message before responsing:

> Well, it would say that if it weren't somewhat buggy. l[:1]
> doesn't evaluate to a number, but to a list containing one
> number, so the above program doesn't do what you say it does.
>
> It should read something like:
>
>  def my_sum(seq):
>    if len(seq) == 0:
>      return 0
>    else:
>      return seq[0] + my_sum(seq[1:])

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to