[Tutor] recursivity and lists

Gaston aminatane at hotmail.fr
Wed Mar 2 05:24:58 EST 2016


Thanks a lot for your help. This was the problem. I fixed it this way :

def linear_merge(list1, list2):
   if list1==[]: return list2
   elif list2==[]: return list1
   elif list1[-1]>list2[-1]:
     a=list1.pop()
     linear_merge(list1,list2).append(a)
     return linear_merge(list1,list2)
   else:
     a=list2.pop()
     linear_merge(list1,list2).append(a)
     return linear_merge(list1,list2)

I use append and then return the result, as list.append is not a query.

Thank you !

On 03/01/2016 09:40 PM, Danny Yoo wrote:
>
> Also, as a quick note: Python list append is not functional: it 
> mutates rather than returns a useful return value.  You may need to 
> revisit the parts in the code where it assumes a different behavior 
> from functional append.
>
> On Mar 1, 2016 12:36 PM, "Danny Yoo" <dyoo at hashcollision.org 
> <mailto:dyoo at hashcollision.org>> wrote:
>
>
>     >
>     > Problem is that python complains that he cannot know the type of
>     the result of this function (list). In C++ I would specify the
>     type, but from what I understood, Python should not need this.
>     What did I miss ?
>
>     Can you copy the exact stack trace of the error? It'll help:
>     you've interpreted what the error means, which is useful, but it's
>     also important to show the unvarnished output too.  It will let us
>     look and make our own interpretations.
>
>     Good luck!
>



More information about the Tutor mailing list