[Tutor] exercise is recursion
Tue, 21 Nov 2000 11:01:49 -0500
On Tue, 21 Nov 2000 10:18:10 Remco Gerlich wrote:
| On Tue, Nov 21, 2000 at 09:50:35AM -0500, D-Man wrote:
| > Why not
| > def factors( n , highestsofar=2 , factorssofar= ) :
| > ...
| > Then when the client calls it
| > result = factors( number , factorssofar= )
| > The list will be empty. One less arg to pass in the recursion and also
| > the need for the "if ( factorssofar == None )" check. (I know the default
| > is only evaluated once, but that can be useful in this case)
| That's a common gotcha, the  is only evaluated once, when the function
| is defined.
| That means that if you call the function more than once, factorssofar will
| not be an empty list anymore, since you changed it! Don't see how that
| is useful.
I realized that. That's why I said the client would pass it the empty list when
it called it, or maybe make a function that set's it to the empty list. It
would be useful b/c the function doesn't have to pass itself the modified list.
Just a tradeoff and style decision.
| Moshe's factorssofar=None (and then set it to  if still None) is the
| standard workaround.
| > Does python do tail-recursion yet? If so, then Moshe's is definitely more
| > efficient (read better). If not, I think the previous solution is easier
| > read, but both are quite clear.
| Python does tail recursion just fine, just not tail recursion *optimization*
Yes, I meant *optimization*. :-)
| Remco Gerlich