Help- Simple recursive function to build a list

2005-03-01 Thread actuary77
I am trying to write simple recursive function to build a list: def rec(n,alist=[]): _nl=alist[:] print n,_nl if n == 0: print n,_nl return _nl else: _nl=_nl+[n] rec(n-1,_nl) _nl=[] _nl=rec(4) print _nl ### shouldn't this work? _nl=rec(4) The output

Re: Help- Simple recursive function to build a list

2005-03-01 Thread Roy Smith
In article [EMAIL PROTECTED], actuary77 [EMAIL PROTECTED] wrote: I am trying to write simple recursive function to build a list: def rec(n,alist=[]): _nl=alist[:] print n,_nl if n == 0: print n,_nl return _nl else: _nl=_nl+[n]

Re: Help- Simple recursive function to build a list

2005-03-01 Thread Kent Johnson
actuary77 wrote: I am trying to write simple recursive function to build a list: def rec(n,alist=[]): _nl=alist[:] print n,_nl if n == 0: print n,_nl return _nl else: _nl=_nl+[n] rec(n-1,_nl) should be return rec(n-1,_nl) def

Re: Help- Simple recursive function to build a list - Sorry for all the noise!

2005-03-01 Thread actuary77
actuary77 wrote: I am trying to write simple recursive function to build a list: def rec(n,alist=[]): _nl=alist[:] print n,_nl if n == 0: print n,_nl return _nl else: _nl=_nl+[n] rec(n-1,_nl) _nl=[] _nl=rec(4) print _nl ### shouldn't this work?

Re: Help- Simple recursive function to build a list - Sorry for all the noise!

2005-03-01 Thread Heiko Wundram
On Wednesday 02 March 2005 06:03, actuary77 wrote: It now makes sense if I write it, (simple): def rec2(n): if n == 0: return [] else: return [n] + rec2(n-1) Or, if you're not interested in a recursive function to do this job (which should be way faster...):