A replacement for lambda

Kay Schluehr kay.schluehr at gmx.net
Sat Jul 30 05:33:13 EDT 2005


Mike Meyer schrieb:

> I know, lambda bashing (and defending) in the group is one of the most
> popular ways to avoid writing code. However, while staring at some Oz
> code, I noticed a feature that would seem to make both groups happy -
> if we can figure out how to avoid the ugly syntax.
>
> This proposal does away with the well-known/obscure "lambda"
> keyword. It gives those who want a more functional lambda what they
> want. It doesn't add any new keywords. It doesn't add any new magic
> characters, though it does add meaning to an existing one. That could
> be replaced by a new magic token, or adding magic meaning to a
> non-magic token. It breaks no old code either way.

Mike,

in modern functional language design one starts with certain lambda
expressions and add syntax sugar ( operational / statement syntax ) for
constructs based on them. In Python statement- and expression syntax
were introduced seperately. Pythons lambda is simply the symptom of
this separation. Now you suggest ( not the first time on this list or
at python-dev ) to put statements into expressions leading to a strange
syntax and conflicting with Pythons indentation rules.

Another way to deal with the restrictions of lambda is going the other
way round and simply propose expression syntax for conds and
assignments.

Using guards '||' and the keyword 'then' for conditional expressions:

           ( || x>=0 then f(x) || True then f(-x) )

Or shorter dropping 'then' in the second condition:

            ( || x>=0 then f(x) || f(-x) )

Both translates to:

              if x>=0:
                 f(x)
              else:
                 f(-x)

Using a reverse arrow for assignments:

             x <- y

For loops can be replaced by functional constructs ( use map() or a
list/generator comprehension ).

Finally the lambda keyword can be replaced by expressional syntax e.g.
( EXPR from ARGS ):

Examples:
   f = ( || x>=0 then f(x) || True then f(-x)  from (x,) )
   g = ( || x< 0 then self._a <-x || self._a <- 0 from (x,))

Kay




More information about the Python-list mailing list