if as an operator?

Robin Munn rmunn at pobox.com
Thu Jan 10 11:25:04 EST 2002


On Thu, 10 Jan 2002 15:37:41 -0800, Giorgi Lekishvili <gleki at gol.ge> wrote:
>Hi!
>
>As most of us knows, scheme programming language presents a function as
>a list.
>
>On a grate scale, our Python has many features of a functional
>programming language, and first of all, that's the support of the lambda
>calculus.
>
>If we apply the operator module, we can also present a simple function
>as a list:
>
>>>> import operator as o
>>>> add=o.add
>>>> sub=o.sub
>>>> div=o.div
>>>> a,b,c=3,5,7
>>>> f=[add,a,[sub,b,c]]
>>>> f
>[<built-in function add>, 3, [<built-in function sub>, 5, 7]]
>>>> print f[0](f[1],f[2][0](f[2][1],f[2][2]))
>1
>>>>  f[2][0]=div
>>>> print f[0](f[1],f[2][0](f[2][1],f[2][2]))
>3
>>>> f[2][1]=5.0
>>>> print f[0](f[1],f[2][0](f[2][1],f[2][2]))
>3.71428571429
>>>>
>
>That means that Python can be very successfully employed for the
>Gennetic Programming, if only one could have at hand 'if' as the
>operator, e.g., like this:
>>>> if_=o.if         #doesn't work now, of course
>Even more important is to make comparisons in this vay:
>>>> more_=o.more        # a would-be analog of '>'
>>>>less_=o.less            # a would-be analog of '<'
>etc.
>
>Of course, we can use operator.truth instead of 'if', but how about the
>comparison?
>
>Mayba, I have missed something?

Do the lt(a,b), le(a,b), eq(a,b), ... functions in the operator module
not do what you want? Ah, I just saw in the Python documentation that
these functions are new in Python 2.2. So it looks like you have your
wish now. Just upgrade to version 2.2 and all your problems will be
solved! :)

>
>
>I believ that the complete, though optional support of functional
>programming in Python would make it even more powerful and useful.
>
>I am familiar with the basics of compiler/interpreter design, but 'm a
>chemist, working in the field of data analysis, not a programmer, as you
>have surely noticed:)
>
>Greetings,
>Giorgi
>
>
>
>


-- 
Robin Munn
rmunn at pobox.com



More information about the Python-list mailing list