[Edu-sig] More Pythonic Precalculus (Python 2.0)
Mon, 25 Sep 2000 12:11:52 -0700
Actually that's not quite what lambda is meant for as you've
gone ahead and named the function anyway (f). So much for
lambda providing "anonymous function" capability.
Might just as well go:
>>> def f(x): return 2 * x * x - 10 * x + 2
(which is the approach I take earlier in my precalc.py).
One reason to allow text in particular is you might have a
front end text box (e.g. in Tk) where students get to
freehand their algebraic rule i.e. don't have direct access
to the command line interface. Another reason is rules-as
-strings are easily save out to file e.g. you could have a
text file like:
and read in each line for successive evaluation (the sin
string will work if you've done your 'from math import *'
In any case, the challenge before me was to accept algebraic
expressions as strings, for whatever reasons.
At 01:15 PM 09/25/2000 -0500, you wrote:
>>>> f = lambda x : 2 * x * x - 10 * x + 2
> for x in range(-10, 10):
> print x, fn(x)
>or the like.
>*much* cleaner; this is *precisely* what lambda is meant for.