Anonymous user
Runtime evaluation/In an environment: Difference between revisions
Runtime evaluation/In an environment (view source)
Revision as of 07:24, 17 February 2009
, 15 years ago→{{header|Common Lisp}}: Python
m (that silly </lang foo> again) |
(→{{header|Common Lisp}}: Python) |
||
Line 28:
(- at-b at-a)))
</lang>
=={{header|Python}}==
Python 2.x's input statement will evaluate its input string as an expression, so by supplying a lambda expression to generate a function we get:
<lang python>func = input('f(x): ')
x = [input('x[%i]: ' % i) for i in [0,1]]
print "f({x[0]}) = {f0}; f({x[1]}) = {f1}; f({x[1]}) - f({x[0]}) = {ans}".format(
x=x, f0=func(x[0]), f1=func(x[1]), ans= func(x[1]) - func(x[0]) )</lang>
Typical use would be <small>(user input is after the initial colon in a line)</small>:
<lang python>f(x): lambda x: 2**x
x[0]: 3
x[1]: 5
f(3) = 8; f(5) = 32; f(5) - f(3) = 24</lang>
|