Tail recursion Re: list of polynomial functions

2006-06-20 Thread Pekka Karjalainen
In article [EMAIL PROTECTED], Matteo wrote: This last approach could, at least theoretically, create an arbitrarily long list of polys, without overflowing any kind of stack. In practice, python does not seem to perform tail recursion optimizations, and conks out after makepolys(997) on my

Re: Tail recursion Re: list of polynomial functions

2006-06-20 Thread tjreedy
Pekka Karjalainen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Python doesn't do any kind of tail recursion optimization by default because Guido has decided it shouldn't (at least so far). I'm sure it has been discussed in detail in Python development forums lists, but I

Re: list of polynomial functions

2006-06-19 Thread Matteo
Josiah Manson wrote: In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am attempting to make a list of polynomial functions such that poly[0](3) = 1, poly[1

Re: list of polynomial functions

2006-06-16 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Tim Chase wrote: My understanding is that the lambda-defined functions are not called until the actual application thereof, with the mypolys = make_polys(8) mypolys[5](2) #the lambda call happens here, no? Yes, that's right. /F's original statement read

list of polynomial functions

2006-06-15 Thread Josiah Manson
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am attempting to make a list of polynomial functions such that poly[0](3) = 1, poly[1](3) = 3, poly[2](3) = 9

Re: list of polynomial functions

2006-06-15 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Josiah Manson wrote: def make_polys(n): Make a list of polynomial functions up to order n. p = lambda x: 1 polys = [p] for i in range(n): polys.append(lambda x: polys[i](x)*x) The `i` is the problem. It's not evaluated

Re: list of polynomial functions

2006-06-15 Thread Tim Chase
The `i` is the problem. It's not evaluated when the lambda *definition* is executed but when the lambda function is called. And then `i` is always == `n`. You have to explicitly bind it as default value in the lambda definition: polys.append(lambda x, i=i: polys[i](x)*x) Then it

Re: list of polynomial functions

2006-06-15 Thread Fredrik Lundh
Tim Chase wrote: The `i` is the problem. It's not evaluated when the lambda *definition* is executed but when the lambda function is called. And then `i` is always == `n`. You have to explicitly bind it as default value in the lambda definition: polys.append(lambda x, i=i:

Re: list of polynomial functions

2006-06-15 Thread Scott David Daniels
will have the same value for all lambdas. There's some subtle behavior here that I'm missing. lexical closures bind to names, default arguments bind to values. Just to be a bit more explicit: In code like: def make_polys(n): Make a list of polynomial functions up to order n

Re: list of polynomial functions

2006-06-15 Thread Josiah Manson
The `i` is the problem. It's not evaluated when the lambda *definition* is executed but when the lambda function is called. And then `i` is always == `n`. You have to explicitly bind it as default value in the lambda definition: polys.append(lambda x, i=i: polys[i](x)*x)

Re: list of polynomial functions

2006-06-15 Thread Tim Chase
Just to be a bit more explicit: In code like: def make_polys(n): Make a list of polynomial functions up to order n. p = lambda x: 1 polys = [p] for i in range(n): polys.append(lambda x: polys[i](x)*x) i=3 The lambda-defined

Re: list of polynomial functions

2006-06-15 Thread Josiah Manson
I'm curious why the very first attempt to call p(3) doesn't bomb out with the NameError that polys wasn't defined before it even got to the point of attempting to call it. In the first call, the 0th lambda function is evaluated, and it was defined as the constant function 1. The functions