lambda (and reduce) are valuable
Peter Otten
__peter__ at web.de
Sat Dec 10 04:27:09 EST 2005
Alan aka David Isaac wrote:
>>> #evaluate polynomial (coefs) at x using Horner's rule
>>> def horner(coefs,x): return reduce(lambda a1,a2: a1*x+a2,coefs)
> It just cannot get simpler or more expressive.
But is it correct?
>>> a0, a1, a2 = 1, 2, 3
>>> x = 2
>>> a0 + x*(a1 + x*(a2))
17
>>> def horner(coefs, x): return reduce(lambda a1, a2: a1*x + a2, coefs)
...
>>> horner([a0, a1, a2], x)
11
Are we merely employing different conventions for the order of coefficients
or is that simple and expressive lambda/reduce stuff obscuring an error?
Peter
More information about the Python-list
mailing list