Re: Iterative vs. Recursive coding

2010-09-06 Thread Aahz
In article <4c70344a$0$1659$742ec...@news.sonic.net>, John Nagle wrote: > >Realistically, recursion isn't that important in Python. It's >there if you need it, and sometimes useful, but generally not used >much without good reason. In some functional languages, recursion >is routinely used

Re: Iterative vs. Recursive coding

2010-09-06 Thread Aahz
In article <4c6e9de9$0$23142$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >Steven D'Aprano a écrit : >> On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: >>> >>> Recursion can be quite a trick to get your mind round at first >> >> Really? Do people actually find the *concept* of

Re: Iterative vs. Recursive coding

2010-08-26 Thread John Bokma
Bruno Desthuilliers writes: > BartC a écrit : >> "Steven D'Aprano" wrote in >> message news:4c6f8edd$0$28653$c3e8...@news.astraweb.com... >>> On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: >> I onced worked in a shop (Win32 desktop / accouting applications mainly) where

Re: Iterative vs. Recursive coding

2010-08-26 Thread Bruno Desthuilliers
BartC a écrit : "Steven D'Aprano" wrote in message news:4c6f8edd$0$28653$c3e8...@news.astraweb.com... On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: I onced worked in a shop (Win32 desktop / accouting applications mainly) where I was the only guy that could actually understan

Re: Iterative vs. Recursive coding

2010-08-25 Thread BartC
"Steven D'Aprano" wrote in message news:4c6f8edd$0$28653$c3e8...@news.astraweb.com... On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: I onced worked in a shop (Win32 desktop / accouting applications mainly) where I was the only guy that could actually understand recursion. FWIW

Re: Iterative vs. Recursive coding

2010-08-24 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: Steven D'Aprano a écrit : On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: Recursion can be quite a trick to get your mind round at first Really? Do people actually find the *concept* of recur

Re: Iterative vs. Recursive coding

2010-08-22 Thread John Nagle
On 8/21/2010 8:32 PM, Steven D'Aprano wrote: On Sat, 21 Aug 2010 19:09:52 -0500, John Bokma wrote: this means that Python should eliminate / optimize tail recursion. There have been various suggestions to add tail recursion optimization to the language. Two problems: * It throws away inform

Re: Iterative vs. Recursive coding

2010-08-22 Thread John Bokma
Steven D'Aprano writes: > On Sat, 21 Aug 2010 19:09:52 -0500, John Bokma wrote: > >> this means that Python should eliminate / optimize tail >> recursion. > > There have been various suggestions to add tail recursion optimization to > the language. Two problems: [snip] > But this is not the on

Re: Iterative vs. Recursive coding

2010-08-22 Thread Hrvoje Niksic
Steven D'Aprano writes: > * It throws away information from tracebacks if the recursive function > fails; and [...] > If you're like me, you're probably thinking that the traceback from an > exception in a recursive function isn't terribly useful. Agreed. On the other hand, a full-fledged tai

Re: Iterative vs. Recursive coding

2010-08-21 Thread Steven D'Aprano
On Sat, 21 Aug 2010 19:09:52 -0500, John Bokma wrote: > this means that Python should eliminate / optimize tail > recursion. There have been various suggestions to add tail recursion optimization to the language. Two problems: * It throws away information from tracebacks if the recursive funct

Re: Iterative vs. Recursive coding

2010-08-21 Thread John Bokma
John Nagle writes: > On 8/20/2010 1:17 PM, John Bokma wrote: >> John Nagle writes: >> >>> Python does not do tail recursion, so using recursion >>> where iteration could do the job is generally a bad idea. Scheme, on >>> the other hand, always does tail recursion where possible. >> >> I th

Re: Iterative vs. Recursive coding

2010-08-21 Thread John Nagle
On 8/21/2010 10:08 AM, Michael Torrie wrote: On 08/21/2010 03:03 AM, Steven D'Aprano wrote: - there must be a condition where the recursion has to stop otherwise the routine will continue to call itself infinitely. This is called the Base Case I agree with this, although I've never heard th

Re: Iterative vs. Recursive coding

2010-08-21 Thread Michael Torrie
On 08/21/2010 03:03 AM, Steven D'Aprano wrote: >> - there must be a condition where the recursion has to stop otherwise >> the routine will continue to call itself infinitely. >> This is called the Base Case > > I agree with this, although I've never heard the name "Base Case" before. "Base Cas

Re: Iterative vs. Recursive coding

2010-08-21 Thread Ian
On 21/08/2010 01:24, Martin Gregorie wrote: On Fri, 20 Aug 2010 16:22:44 -0700, Baba wrote: > For the purposes of learning programming i think it's a must to > understand Recursion so thanks all for your help! > That depends on the language and/or hardware. COBOL wouldn't understand recursi

Re: Iterative vs. Recursive coding

2010-08-21 Thread Ian
On 21/08/2010 00:17, Ben Finney wrote: Steven D'Aprano writes: On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: Recursion can be quite a trick to get your mind round at first Really? Do people actually find the *concept* of recursion to be tricky? Evidently so. It's folk wisdom

Re: Re: Iterative vs. Recursive coding

2010-08-21 Thread Dave Angel
Baba wrote: On Aug 21, 7:37 am, John Nagle wrote: On 8/20/2010 1:17 PM, John Bokma wrote: I think you mean tail recursion optimization / elimination. Python does tail recursion: Not very well. def cnt(n) : if n > 0 : cnt(n-1) Hi John I'm

Re: Iterative vs. Recursive coding

2010-08-21 Thread Tim Chase
On 08/21/10 03:31, Steven D'Aprano wrote: On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: I also was the only guy around that understood "hairy" (lol) concepts like callback functions, FSM, FSM? Flying Spaghetti Monster? I'm guessing "Finite State Machines". But in a way, "Fl

Re: Iterative vs. Recursive coding

2010-08-21 Thread Tim Chase
On 08/21/10 04:35, Baba wrote: On Aug 21, 7:37 am, John Nagle wrote: On 8/20/2010 1:17 PM, John Bokma wrote: I think you mean tail recursion optimization / elimination. Python does tail recursion: Not very well. def cnt(n) : if n> 0 : cnt(n-1) I'm intri

Re: Iterative vs. Recursive coding

2010-08-21 Thread Baba
On Aug 21, 7:37 am, John Nagle wrote: > On 8/20/2010 1:17 PM, John Bokma wrote: > > > I think you mean tail recursion optimization / elimination. > > Python does tail recursion: > >     Not very well. > >      def cnt(n) : >          if n > 0 : >              cnt(n-1) > Hi John I'm intrigued by

Re: Iterative vs. Recursive coding

2010-08-21 Thread Steven D'Aprano
On Fri, 20 Aug 2010 16:42:26 -0700, Baba wrote: > For future readers of this post who want to learn to programm (just like > myself) let me re-state the basics i have learned now: I would disagree in part with nearly all of these. > - a procedure is said to be recursive when it contains a statem

Re: Iterative vs. Recursive coding

2010-08-21 Thread Steven D'Aprano
On Fri, 20 Aug 2010 17:23:23 +0200, Bruno Desthuilliers wrote: > Steven D'Aprano a écrit : >> On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: >> >>> Recursion can be quite a trick to get your mind round at first >> >> Really? Do people actually find the *concept* of recursion to be >>

Re: Iterative vs. Recursive coding

2010-08-21 Thread Steven D'Aprano
On Fri, 20 Aug 2010 09:47:30 +0200, News123 wrote: > On 08/20/2010 02:26 AM, Steven D'Aprano wrote: >> On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: >> >>> Recursion can be quite a trick to get your mind round at first >> >> Really? Do people actually find the *concept* of recursion

Re: Iterative vs. Recursive coding

2010-08-20 Thread John Nagle
On 8/20/2010 1:17 PM, John Bokma wrote: John Nagle writes: Python does not do tail recursion, so using recursion where iteration could do the job is generally a bad idea. Scheme, on the other hand, always does tail recursion where possible. I think you mean tail recursion optimization

Re: Iterative vs. Recursive coding

2010-08-20 Thread Martin Gregorie
On Fri, 20 Aug 2010 16:22:44 -0700, Baba wrote: > For the purposes of learning programming i think it's a must to > understand Recursion so thanks all for your help! > That depends on the language and/or hardware. COBOL wouldn't understand recursion if hit on the head with a recursion brick and e

Re: Iterative vs. Recursive coding

2010-08-20 Thread Thomas Jollans
On Saturday 21 August 2010, it occurred to Baba to exclaim: > - every time the procedure calls itself the memory gradually fills up > with the copies until the whole thing winds down again > as the "return" statements start being executed. > - the above point means that a recursive approach is ex

Re: Iterative vs. Recursive coding

2010-08-20 Thread Baba
On Aug 19, 11:00 pm, Martin Gregorie wrote: > By way of a hint, here are two versions of the classic example of > recursion: calculating factorials. Recursion can be quite a trick to get > your mind round at first, so compare the two and follow through their > operation step by step... Hi Martin

Re: Iterative vs. Recursive coding

2010-08-20 Thread Baba
Hi Martin Thanks for your post. This basic but fundamental computation is a great example when trying to understand the concept of recursion for the first time. Also thanks to John for the stackoverflow link where i found a very good summarised definition completing some of the posts left here.

Re: Iterative vs. Recursive coding

2010-08-20 Thread Ben Finney
Steven D'Aprano writes: > On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: > > > Recursion can be quite a trick to get your mind round at first > > Really? Do people actually find the *concept* of recursion to be > tricky? Evidently so. It's folk wisdom that some adults find recursion

Re: Iterative vs. Recursive coding

2010-08-20 Thread John Bokma
John Nagle writes: > Python does not do tail recursion, so using recursion > where iteration could do the job is generally a bad idea. Scheme, on > the other hand, always does tail recursion where possible. I think you mean tail recursion optimization / elimination. Python does tail recursi

Re: Iterative vs. Recursive coding

2010-08-20 Thread Neil Cerutti
On 2010-08-20, John Nagle wrote: > Python does not do tail recursion, so using recursion where > iteration could do the job is generally a bad idea. Scheme, on > the other hand, always does tail recursion where possible. A tail-recursive function is usually easy to convert to a loop-style iterat

Re: Iterative vs. Recursive coding

2010-08-20 Thread John Nagle
On 8/20/2010 12:47 AM, News123 wrote: On 08/20/2010 02:26 AM, Steven D'Aprano wrote: On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: Recursion can be quite a trick to get your mind round at first Really? Do people actually find the *concept* of recursion to be tricky? Is this a s

Re: Iterative vs. Recursive coding

2010-08-20 Thread Bruno Desthuilliers
Michel Claveau - MVP a écrit : Salut ! C'est cela, la solitude du programmeur génial... @-salutations Moi aussi je t'aime, Michel !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterative vs. Recursive coding

2010-08-20 Thread Michel Claveau - MVP
Salut ! C'est cela, la solitude du programmeur génial... @-salutations -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterative vs. Recursive coding

2010-08-20 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: Recursion can be quite a trick to get your mind round at first Really? Do people actually find the *concept* of recursion to be tricky? I onced worked in a shop (Win32 desktop / accouting applications main

Re: Iterative vs. Recursive coding

2010-08-20 Thread geremy condra
On Fri, Aug 20, 2010 at 12:47 AM, News123 wrote: > On 08/20/2010 02:26 AM, Steven D'Aprano wrote: >> On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: >> >>> Recursion can be quite a trick to get your mind round at first >> >> Really? Do people actually find the *concept* of recursion to

Re: Iterative vs. Recursive coding

2010-08-20 Thread Navkirat Singh
On 20-Aug-2010, at 1:17 PM, News123 wrote: > On 08/20/2010 02:26 AM, Steven D'Aprano wrote: >> On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: >> >>> Recursion can be quite a trick to get your mind round at first >> >> Really? Do people actually find the *concept* of recursion to be

Re: Iterative vs. Recursive coding

2010-08-20 Thread News123
On 08/20/2010 02:26 AM, Steven D'Aprano wrote: > On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: > >> Recursion can be quite a trick to get your mind round at first > > Really? Do people actually find the *concept* of recursion to be tricky? Is this a sincere surprise or are you just b

Re: Iterative vs. Recursive coding

2010-08-19 Thread John Nagle
On 8/19/2010 2:41 PM, Thomas Jollans wrote: On Thursday 19 August 2010, it occurred to Baba to exclaim: This is not recursive. In fact, it's exactly the same approach as the first one, plus a bit of an if statement. Right. The original poster seems to be getting their ideas from "http:/

Re: Iterative vs. Recursive coding

2010-08-19 Thread Steven D'Aprano
On Thu, 19 Aug 2010 22:00:16 +, Martin Gregorie wrote: > Recursion can be quite a trick to get your mind round at first Really? Do people actually find the *concept* of recursion to be tricky? If I remember correctly, my puzzlement about recursion lasted about 15 seconds. I remember thinkin

Re: Iterative vs. Recursive coding

2010-08-19 Thread MRAB
Thomas Jollans wrote: [snip] "iterate": use a loop. and again. and again. and again. and again. and aga "recurse": consider. recurse. This is another good one: http://mytechquest.com/blog/wp-content/uploads/2010/05/From-a-Programming-Book.jpg The definition for recursion looks a lot li

Re: Iterative vs. Recursive coding

2010-08-19 Thread Dave Angel
others, with the ideas of iterative vs. recursive coding approaches and i was wondering what are the advantages of each and how to best chose between both options? I had a go at the first part of the exercise and it seems that i can handle it. However i think my Recursive version can be improved. By

Re: Iterative vs. Recursive coding

2010-08-19 Thread Martin Gregorie
rst problem in the above assignment. The > assignemnt deals, amongst others, with the ideas of iterative vs. > recursive coding approaches and i was wondering what are the advantages > of each and how to best chose between both options? > > I had a go at the first part of the exercise

Re: Iterative vs. Recursive coding

2010-08-19 Thread Daniel Kluev
problem in the above assignment. The > assignemnt deals, amongst others, with the ideas of iterative vs. > recursive coding approaches and i was wondering what are the > advantages of each and how to best chose between both options? > > With Python, I'd avoid using recursi

Re: Iterative vs. Recursive coding

2010-08-19 Thread MRAB
others, with the ideas of iterative vs. recursive coding approaches and i was wondering what are the advantages of each and how to best chose between both options? I had a go at the first part of the exercise and it seems that i can handle it. However i think my Recursive version can be improved. By

Re: Iterative vs. Recursive coding

2010-08-19 Thread Thomas Jollans
On Thursday 19 August 2010, it occurred to Baba to exclaim: > def countSubStringMatchRecursive(target,key): > counter=0 > fsi=0 #fsi=find string index > if len(key)==len(target): #base case > if key==target: >counter+=1 > elif len(key) while fsi

Iterative vs. Recursive coding

2010-08-19 Thread Baba
ideas of iterative vs. recursive coding approaches and i was wondering what are the advantages of each and how to best chose between both options? I had a go at the first part of the exercise and it seems that i can handle it. However i think my Recursive version can be improved. By the way, is my