if as an operator?

Giorgi Lekishvili gleki at gol.ge
Thu Jan 10 18:37:41 EST 2002


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?


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







More information about the Python-list mailing list