question about what lamda does

danielx danielwong at berkeley.edu
Thu Jul 20 17:02:19 EDT 2006


Bruno Desthuilliers wrote:
> danielx wrote:
> (snip)
>
> > Python's lambda really can't be as powerful as Lisp's because Python
> > does not have expressions that do case analysis (this is not lambda's
> > fault, of course ;). The reason is that you really want to put each
> > case on its own set of lines. This enhances readability at the expense
> > of terseness. Since Python's statements are terminated by a newline, it
> > would be rather awkward to have a kind of expression where good style
> > calls for it to be spread out accross multiple lines.
> >
> > You can try to simulate these kinds expressions using into a list or
> > dictionary, but this becomes rather messy. I think the only way to get
> > this done properly is to use eval. For example:
> >
> > def recursiveFunction(args):
> >   ...  # do stuff...
> >   choices = { True:"0", False:"recurisveFunction(newArgs)" }
> >   return eval( choices[predicate] )
>
> Why do you want to use eval here ?
>
> > The reason that you need eval is that you want to prevent any cases
> > from being executed until you decide which one you want.
>
> What about:
>
> def recursiveFunction(args):
>     ... # do stuff...
>     ... # that defines 'newArgs' and 'predicate' of course ...
>     return (recursiveFunction, lambda x: 0)[predicate](newArgs)

Sure, that works, but don't take things so literally. For instance, if
you have a bunch of cases, you might not way to apply the same set of
arguments to all of them.

Also, let's not get distracted from the main point about how doing case
analysis in an expression is ugly, making lambda's weaker in Python
than in the language which inspired them.

>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in 'onurb at xiludom.gro'.split('@')])"




More information about the Python-list mailing list