473,773 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursion

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?

thanks!
Sep 13 '07
15 1448
Steve Holden wrote:
Gigs_ wrote:
>sorry i think that i express wrong. having problem with english
what i mean is how python knows to add all thing at the end of recursion
> >>def f(l):
if l == []:
return []
else:
return f(l[1:]) + l[:1]
f([1,2,3])

recursion1 f([2,3]) + [1]

recursion2 f([3]) + [2] or [2, 1]?

recursion3 f([]) + [3] or [3, 2, 1]
i dont get all this
> >>def f(l):
if l == []:
print l
return []
else:
return f(l[1:]) + l[:1]
> >>f([1,2,3])
[]
[3, 2, 1] # how this come here? how python save variables from each
recursion?
sorry again for first post
I think the thing you are missing is that the recursive call f(l[1:]) in
the return statement causes the current call to be suspended until the
recursive call is complete. The new call has its own value for l, which
is the caller's l[1:]. Each new call creates a completely new namespace.

A less complicated function might make it a little more obvious.

def factorial(n):
print "n =", n
if n=0:
return 1
else:
return n * factorial(n-1)

Try running that with a few different arguments to show you how the
recursion works.

regards
Steve
>>def factorial(n):
print "n =", n
if n==0:
return 1
else:
return n * factorial(n-1)
>>factorial(3 )
n = 3
n = 2
n = 1
n = 0
6
now i understand. but one question at the end this function return 1. how python
knows that it needs to multiply 1 with all recursive returns. (why python didnt
sum recursive return with 1?)
that will be all, thanks in advance
Sep 14 '07 #11

On Fri, Sep 14, 2007 at 01:40:17PM +0200, Gigs_ wrote regarding Re: recursion:
>
what i mean is how python knows to add all thing at the end of recursion
>>def f(l):
if l == []:
return []
else:
return f(l[1:]) + l[:1]
The following script does exactly the same thing, except it creates print
statements to help you figure out what's going on, and it binds f(L[1:]) to
a variable so you can use it again.

def f(L): # l capitalized to accentuate difference between l and 1.
print "L =", L
print "L[1:] =", L[1:]
print "L[:1] =", L[:1]
if L == []:
print "Return: ", []
return []
else:
next = f(L[1:])
print "Return: ", next, "+", L[:1], "=", next + L[:1]
return next + L[:1]

if __name__=='__ma in__':
print f(['A', 'B', 'C', 'D'])

Try it out. See what happens.

Cheers,
Cliff
Sep 14 '07 #12
On 2007-09-14, John Machin <sj******@lexic on.netwrote:
On Sep 14, 10:04 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
>On Fri, 14 Sep 2007 13:40:17 +0200, Gigs_ wrote:
sorry i think that i express wrong. having problem with english
what i mean is how python knows to add all thing at the end of recursion

Because you have written code that tells Python to do so. ;-)
>>def f(l):
if l == []:
return []
else:
return f(l[1:]) + l[:1]
f([1,2,3])
recursion1 f([2,3]) + [1]
recursion2 f([3]) + [2] or [2, 1]?
recursion3 f([]) + [3] or [3, 2, 1]

Both alternatives in recursion 2 and 3 are wrong. You have to
simply replace the function invocation by its result which
gives:

f([1, 2, 3])
r1 f([2, 3]) + [1]
r2 f([3]) + [2] + [1]
r3 f([]) + [3] + [2] + [1]
r4 [] + [3] + [2] + [1]

And now the calls return:

r3 [3] + [2] + [1]
r2 [3, 2] + [1]
r1 [3, 2, 1]
i dont get all this
>>def f(l):
if l == []:
print l
return []
else:
return f(l[1:]) + l[:1]
>>f([1,2,3])
[]
[3, 2, 1] # how this come here? how python save variables from each
recursion?

There is not just one `l` but one distinct `l` in each call.

I reckon that what the OP wants is a simple explanation of how
function calls use a stack mechanism for arguments and local
variables, and how this allows recursive calls, unlike the good ol'
FORTRAN IV of blessed memory. Perhaps someone could oblige him?

I'd try but it's time for my beauty sleep :-)
<yawn>
Good night all
I may as well stick my neck out again, since I'm already
beautiful. ;)

Another way of understanding recursion is to break it up into
seperate functions, so the spectre of a function calling itself
doesn't appear.

def f(l):
if l == []:
return []
else:
return f(l[1:]) + l[:1]

The function above reverses a list of arbitrary length. To help
understand how it works, I'll write several discreet functions
that sort lists of fixed lengths.

I start with a simple case (though not the simplest case--that
only comes with experience), reversing a two-element list:

def f2(l): # reverse a two-element list
return [l[1], l[0]]

Next build up to the next level, writing a function that can
reverse a three-element list. The key is to be as lazy as
possible. You must figure out a way of taking advantage of the
function that reverses a two-element list. The obvious solution
is to use f2 to reverse the last two elements in our list, and
append the first element in the list to that result:

def f3(l): # reverse a three-element list
return f2(l[1:]) + l[:1]

And so on:

def f4(l):
return f3(l[1:]) + l[:1]

def f5(l):
return f4(l[1:]) + l[:1]

def f6(l):
return f5(l[1:]) + l[:1]

A definite pattern had emerged, and it should be apparent now how
to combine all those functions into one:

def f_(l):
if len(l) == 2:
return [l[1], l[0]]
else:
return f_(l[1:]) + l[:1]

But the function above breaks for lists with less than two items.
>>f_([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f2
IndexError: list index out of range

We can handle that. The reverse of a zero or one-element list is
just itself.

def f_(l):
if len(l) < 2:
return l
elif len(l) == 2:
return [l[1], l[0]]
else:
return f_(l[1:]) + l[:1]

And we've arrived at an OK recursive function that can handle
arbitrary length lists. It's not as simple as it could be,
however. A little intuitive leap, perhaps, will allow you to note
that the case of a two-element list can actually be handled
without a special case:

def f(l):
if len(l) < 2:
return l
else:
return f(l[1:]) + l[:1]

Final note: for reasons of tradition, base cases are almost
always set up as it was in the original function, checking for a
zero-length list, and returning a new empty list, the truly
simplest base case. Another intuitive leap is possibly required
to note that a one-element list is not a special case after all:
it's a reverse of a zero-element list with that one element
appended.

def f(l):
if len(l) == 0:
return []
else:
return f(l[1:]) + l[:1]

Clear as mud?

--
Neil Cerutti
Sep 14 '07 #13
On Fri, 14 Sep 2007 15:58:39 +0200, Gigs_ wrote:
>>def factorial(n):
print "n =", n
if n==0:
return 1
else:
return n * factorial(n-1)
>>factorial(3 )
n = 3
n = 2
n = 1
n = 0
6
now i understand. but one question at the end this function return 1. how python
knows that it needs to multiply 1 with all recursive returns. (why python didnt
sum recursive return with 1?)
Because there is a ``*`` and not a ``+`` in the last line of the function.

Let's play this through (I abbreviate the function name to just `f()`):

Execution of f(3) leads to the second return:

r1 f(3): return 3 * f(2)

This multiplication can't take place until ``f(2)`` is calculated so the
current function call is "suspended" and evaluated later, when the result
of ``f(2)`` is known. The call in that line is replaces with the result
then. Calling ``f(2)`` leads to:

r2 f(2): return 2 * f(1)

The same again, another call to `f()` with 1 as argument:

r3 f(1): return 1 * f(0)

Now the last call takes the other ``if`` branch:

r4 f(0): return 1

The 1 is returned to the previus call:

r3 f(1): return 1 * 1

This can be evaluated now and is returned to its caller:

r2 f(2): return 2 * 1

Again this is evaluated and returned to its caller:

r1 f(3): return 3 * 2

And here we have the final result that is returned from the first call to
`f()`.

Ciao,
Marc 'BlackJack' Rintsch
Sep 14 '07 #14

"Marc 'BlackJack' Rintsch" <bj****@gmx.net wrote in message
news:5k******** ****@mid.uni-berlin.de...
| f([1, 2, 3])
| r1 f([2, 3]) + [1]
| r2 f([3]) + [2] + [1]
| r3 f([]) + [3] + [2] + [1]
| r4 [] + [3] + [2] + [1]

I might help to note that the above is effectively parenthesized

( ( ([]+{3]) + [2]) +[1])

*and* that each addition (in each pair of parentheses) is done
in a different execution frame (see below).

| And now the calls return:
|
| r3 [3] + [2] + [1]
| r2 [3, 2] + [1]
| r1 [3, 2, 1]
| [3, 2, 1] # how this come here? how python save variables from each
| recursion?

*Each time* a function is called, an execution frame is created(1) that is
separate from the function object itself. Each execution frame has its own
set of local variables. In particular, each has its own slices of the
original list.

There have been languages, for instance, Fortran IV, where local variables
were part of the function 'object' and which therefore prohibited recursion
because of the very problem you alluded to in your question. (My guess is
the functions had an InUse flag that was checked when the function was
called.)

tjr

(1) A minor variation would be for function objects to have one
pre-allocated execution frame for non-recursive calls and others allocated
as needed for recursive calls.

Sep 14 '07 #15
On Sep 15, 3:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
>
There have been languages, for instance, Fortran IV, where local variables
were part of the function 'object' and which therefore prohibited recursion
because of the very problem you alluded to in your question. (My guess is
the functions had an InUse flag that was checked when the function was
called.)
No way. Waste of space for instructions to check the flag at the start
and reset it at the end. Waste of CPU time. Divide by zero? Subroutine
calls itself? Drive against the traffic flow on the freeway? Expected
outcome: crash or other unwanted result. Simple: don't do that! Why
bother checking? No self-respecting FORTRAN programmer would want to
use recursion anyway. And mixed case in a variable name? Sheesh.

Sep 14 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3423
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion. And so it says ANTLR. Maybe I don't understand EBNF notation. For me it should look like this. or_expr::= xor_expr | xor_expr "|" xor_expr and in ANTLR grammar file like this:
12
2766
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted in modern day applications. Should we readily use it when we can or only when absolutly forced to use it?
43
4172
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
1569
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm looking for problems that have double (or more) recursion, where the split is not clean (ie. where there may be overlap). Let's call this overlapped recursion, where the
75
5642
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
19
2286
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
3723
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. Any suggestions appreciated!
13
4528
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs? Robert
20
3005
by: athar.mirchi | last post by:
..plz define it.
35
4741
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.