Re: Avoiding argument checking in recursive calls

2009-02-13 Thread Steven D'Aprano
Terry Reedy wrote: > Steven D'Aprano wrote: >> On Wed, 11 Feb 2009 04:31:10 -0500, Terry Reedy wrote: >> Steven D'Aprano writes: > def fact(n): > if n < 0: raise ValueError > if n = 0: return 1 > return fact(n-1)*n > > At the risk of premature optimizatio

Re: Avoiding argument checking in recursive calls

2009-02-12 Thread Terry Reedy
Steven D'Aprano wrote: On Wed, 11 Feb 2009 04:31:10 -0500, Terry Reedy wrote: Steven D'Aprano writes: def fact(n): if n < 0: raise ValueError if n = 0: return 1 return fact(n-1)*n At the risk of premature optimization, I wonder if there is an idiom for avoiding the unnecessary te

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Steven D'Aprano
On Wed, 11 Feb 2009 04:31:10 -0500, Terry Reedy wrote: >> Steven D'Aprano writes: >>> def fact(n): >>> if n < 0: raise ValueError >>> if n = 0: return 1 >>> return fact(n-1)*n >>> >>> At the risk of premature optimization, I wonder if there is an idiom >>> for avoiding the unnecessary

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Terry Reedy
andrew cooke wrote: Terry Reedy wrote: Reverse the test order def fact(n): if n > 0: return fact(n-1)*n if n == 0: return 1 raise ValueError sweet! but is this generally possible? I believe so, for some meaning of 'generally'. > ie: did you think this up for this question

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread andrew cooke
Terry Reedy wrote: > Reverse the test order > > def fact(n): > if n > 0: return fact(n-1)*n > if n == 0: return 1 > raise ValueError sweet! but is this generally possible? ie: did you think this up for this question or is it an idiom that you find yourself using often? andrew --

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Jervis Whitley
> > You've merely replaced the 'test n<0' with 'not check' at the expense > of an additional parameter that has to be passed each time (and the > additional test 'n<0' for the first iteration). > -- > http://mail.python.org/mailman/listinfo/python-list > I think you have missed the point. The OP s

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Jervis Whitley
> You've merely replaced the 'test n<0' with 'not check' at the expense > of an additional parameter that has to be passed each time (and the > additional test 'n<0' for the first iteration). > -- > http://mail.python.org/mailman/listinfo/python-list > I think you have missed the point. The OP stat

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Terry Reedy
Steven D'Aprano writes: def fact(n): if n < 0: raise ValueError if n = 0: return 1 return fact(n-1)*n At the risk of premature optimization, I wonder if there is an idiom for avoiding the unnecessary test for n <= 0 in the subsequent recursive calls? Reverse the test order d

Re: Avoiding argument checking in recursive calls

2009-02-11 Thread Aaron Brady
On Feb 10, 7:58 pm, Steven D'Aprano wrote: > I sometimes write recursive functions like this simple factorial: > > def fact(n): >     if n < 0: raise ValueError >     if n = 0: return 1 >     return fact(n-1)*n > > At the risk of premature optimization, I wonder if there is an idiom for > avoiding

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Paul Rubin
Paul Rubin writes: > I'd write nested functions: > > def fact(n): >if n < 0: raise ValueError >def f1(n): > return 1 if n==0 else n*f1(n-1) >return f1(n) I forgot to add: all these versions except your original one should add a

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Paul Rubin
Steven D'Aprano writes: > def fact(n): > if n < 0: raise ValueError > if n = 0: return 1 > return fact(n-1)*n > > At the risk of premature optimization, I wonder if there is an idiom for > avoiding the unnecessary test for n <= 0 in the subsequent recursive > calls? I'd write nest

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Scott David Daniels
Steven D'Aprano wrote: I sometimes write recursive functions like this simple factorial: def fact(n): if n < 0: raise ValueError if n = 0: return 1 return fact(n-1)*n At the risk of premature optimization, I wonder if there is an idiom for avoiding the unnecessary test for n <= 0 i

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread afriere
On Feb 11, 1:48 pm, Jervis Whitley wrote: > Hello, an idea is optional keyword arguments. > > def fact(n, check=False): >   if not check: >     if n < 0: raise ValueError >   if n == 0: return 1 >   return fact(n - 1, check=True) * n > > essentially hiding an expensive check with a cheap one. It

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Gabriel Genellina
En Tue, 10 Feb 2009 23:58:07 -0200, Steven D'Aprano escribió: I sometimes write recursive functions like this simple factorial: def fact(n): if n < 0: raise ValueError if n = 0: return 1 return fact(n-1)*n At the risk of premature optimization, I wonder if there is an idiom for

Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Jervis Whitley
> I've done this: > > def _fact(n): >if n = 0: return 1 >return _fact(n-1)*n > > def fact(n): >if n < 0: raise ValueError >return _fact(n) > > but that's ugly. What else can I do? > > Hello, an idea is optional keyword arguments. def fact(n, check=False): if not check: if n <

Avoiding argument checking in recursive calls

2009-02-10 Thread Steven D'Aprano
I sometimes write recursive functions like this simple factorial: def fact(n): if n < 0: raise ValueError if n = 0: return 1 return fact(n-1)*n At the risk of premature optimization, I wonder if there is an idiom for avoiding the unnecessary test for n <= 0 in the subsequent recurs