Haskell lambdas in python

Michael Speer knomenet at gmail.com
Thu Aug 16 16:22:02 EDT 2007


Hello python-list,

I decided to have some fun and altered a copy of python to use a
Haskell-like syntax for declaring lambdas.

>>> filter( \ x -> x == 3 or x > 8 , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
10 ] )
[3, 9, 10]
>>> ( \-> "Hello World!" )()
'Hello World!'

While doing this I found something interesting while changing the grammar to
:

    old_lambdef: ( 'lambda' | '\\' ) [varargslist] ( ':' | '->' ) old_test
    ...
    lambdef: ( 'lambda' | '\\' ) [varargslist] ( ':' | '->' ) test


In Parser/tokenizer.c, instead of needing to change only the PyToken_OneChar
function to have a
        case '\\':      return LAMBDAARGS;
option, I also had to add
        case '\\':
                switch (c2) {
                case '\\':      return LAMBDAARGS;
                }
to PyToken_TwoChars.

The interactive runtime requires the OneChar definition, and the compilation
process ( specifically pgen ) requires the TwoChar definition.

If anyone wants the changes required to play around with it just email me
and I'll find somewhere to stick them on the web for download.

It seems a waste that whack acts only as an end of line continuance.

- Michael Speer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070816/bdba39f9/attachment.html>


More information about the Python-list mailing list