Re: [Tutor] recursive function example

2013-12-12 Thread Mark Lawrence
On 12/12/2013 04:18, uga...@talktalk.net wrote: I don't mind you asking if you don't understand something, but please don't top post on this list, it makes following discussions such as this more difficult than it need be. -- My fellow Pythonistas, ask not what our language can do for you, as

Re: [Tutor] recursive function example

2013-12-12 Thread Alan Gauld
Oops, I got this slightly wrong. On 12/12/13 08:50, Alan Gauld wrote: mult(3,0) It returns zero because b is zero, right? Now consider what it does for mult(3,1) It checks if b is zero, it's not, so it executes rest = 3 + mult(3,0) Sorry, it actually does: rest = mult(3,0) So rest equal

Re: [Tutor] recursive function example

2013-12-12 Thread Alan Gauld
On 12/12/13 04:18, uga...@talktalk.net wrote: In a way,it may help to identify the issue def multiply(a,b) return a*b clearly returns the product of the two arguments, a and b I presume it returns a+a rather than b+b+b It depends on how multiplication is implemented in the CPU microcode. But

Re: [Tutor] recursive function example

2013-12-12 Thread ugajin
multiply the arguments How in the original def mult(a, b) . . ., does mult(a, b-1) say return the product of a and b-1? -Original Message- From: Alan Gauld To: tutor@python.org Sent: Thu, 12 Dec 2013 0:27 Subject: Re: [Tutor] recursive function example On 11/12/13 18:09, uga

Re: [Tutor] recursive function example

2013-12-11 Thread Alan Gauld
On 11/12/13 18:09, uga...@talktalk.net wrote: No, not really. mutl(3, 2) has two arguments rest = mult(a, b - 1) also has two arguments rest does not have any arguments. arguments are the values you pass *into* a function. The function in turn passes back a return value. In this case rest is

Re: [Tutor] recursive function example

2013-12-11 Thread ugajin
: Re: [Tutor] recursive function example On 10/12/2013 14:48, uga...@talktalk.net wrote: [snipped] As you're clearly struggling here's my attempt at showing you what is happening. c:\Users\Mark\MyPython>type mytest.py level = 0 def mult(a, b): global level

Re: [Tutor] recursive function example

2013-12-11 Thread spir
This is a pretty good clarification! (debug prints well designed and well placed) Congrats, Mark! denis On 12/11/2013 06:37 PM, Mark Lawrence wrote: On 10/12/2013 14:48, uga...@talktalk.net wrote: [snipped] As you're clearly struggling here's my attempt at showing you what is happening. c:

Re: [Tutor] recursive function example

2013-12-11 Thread spir
On 12/11/2013 07:15 PM, Alan Gauld wrote: Remember we are calling mul() several times, each with a new set of values. So mul(3,2) calls mul(3,1) and mul(3,1) calls mul(3,0) mul(3.0) returns 0 to mul(3,1) mul(3,1) then returns 3+0 => 3 to mul(3,2) mul(3,2) returns 3+3 => 6. This is a very cle

Re: [Tutor] recursive function example

2013-12-11 Thread spir
On 12/11/2013 03:56 PM, uga...@talktalk.net wrote: Self-similar (fractal) recursion, sounds complex, I am guessing this is like linear recursion but simultaneously in more than one dimension? Curious business really. Wonders, if I may be a closet programmer, or something, It is not complex,

Re: [Tutor] recursive function example

2013-12-11 Thread Alan Gauld
On 11/12/13 12:36, uga...@talktalk.net wrote: What I do not see is how? Clearly, a = 3, it is constant throughout each iteration, and if rest is equal to 3, then a + rest must be equal to 6. Correct and the return value from the second invocation of mul() is 3. You spoke of drilling down, a

Re: [Tutor] recursive function example

2013-12-11 Thread Mark Lawrence
On 10/12/2013 14:48, uga...@talktalk.net wrote: [snipped] As you're clearly struggling here's my attempt at showing you what is happening. c:\Users\Mark\MyPython>type mytest.py level = 0 def mult(a, b): global level level += 1 print('level now', level, 'a =', a, 'b =', b) if

Re: [Tutor] recursive function example

2013-12-11 Thread ugajin
ow the sub_res outcomes are printed :) -A -Original Message- From: spir To: tutor@python.org Sent: Wed, 11 Dec 2013 13:15 Subject: Re: [Tutor] recursive function example On 12/10/2013 03:48 PM, uga...@talktalk.net wrote: > [...] Recursivity is hard to get really, meaning in

Re: [Tutor] recursive function example

2013-12-11 Thread ugajin
e. I do not understand "+>", is it a typo perhaps? Many thanks. -A -Original Message- From: Alan Gauld To: tutor@python.org Sent: Wed, 11 Dec 2013 9:05 Subject: Re: [Tutor] recursive function example On 10/12/13 14:48, uga...@talktalk.net wrote: OK, I'll try a

Re: [Tutor] recursive function example

2013-12-11 Thread spir
On 12/11/2013 09:50 AM, Alan Gauld wrote: Remember that each time mult() is called it creates its own mini-world of variables independent of the previous calls. That, is a key point. Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or cha

Re: [Tutor] recursive function example

2013-12-11 Thread spir
On 12/10/2013 03:48 PM, uga...@talktalk.net wrote: [...] Recursivity is hard to get really, meaning intuitively with your guts so-to-say. Maybe using another example may help. Lets us say you want a function that sums numbers from 1 up to n, the only input variable. (The result should thus be

Re: [Tutor] recursive function example

2013-12-11 Thread Alan Gauld
On 10/12/13 14:48, uga...@talktalk.net wrote: OK, I'll try again, this time just walking through the code from the top. def mult(a, b): if b == 0: return 0 rest = mult(a, b - 1) value = a + rest return value > print "3 * 2 = ", mult(3, 2) We print "3 * 2 = " and

Re: [Tutor] recursive function example

2013-12-11 Thread ugajin
-Original Message- From: Alan Gauld To: tutor@python.org Sent: Wed, 11 Dec 2013 0:29 Subject: Re: [Tutor] recursive function example On 10/12/13 14:48, uga...@talktalk.net wrote: >> Here is original code: >> def mult(a, b): >> if b == 0: &g

Re: [Tutor] recursive function example

2013-12-10 Thread Alan Gauld
On 10/12/13 14:48, uga...@talktalk.net wrote: Here is original code: def mult(a, b): if b == 0: return 0 rest = mult(a, b - 1) value = a + rest return value print "3 * 2 = ", mult(3, 2) I see how python outputs the string "mult(3,2)" before running the function, N