Python syntax in Lisp and Scheme

David Mertz mertz at gnosis.cx
Thu Oct 16 23:10:52 EDT 2003


|>And you DO NOT NEED lambdas for HOFs!

bokr at oz.net (Bengt Richter) wrote previously:
|there could be ways that you need BOTH named and un-named functions.

Nope, you do not NEED both.  It can be convenient or expressive to have
both, but it is certainly not necessary for HOFs or any other
computational purpose.  And I have yet to see an example where a
hypothetical loss of unnamed functions would *significantly* worsen
expressiveness.

|a function NEEDS a name in order to call itself recursively

Nope.  That's the point of the Y combinator; you don't need a name to do
this (just first class anonymous functions).  See, for example:

    http://en2.wikipedia.org/wiki/Y_combinator

|OTOH, if you evaluate a def in a namespace where you don't know what
|all the names are, you have a collision risk when you choose a name.
|An un-named function eliminates that risk.

Sure, that can occassionally be useful in eliminating the small risk.
But so can 'grep'.  There are always more names out there to use.  This
particular convenience is VERY small.

|Why should the following kind of thing be arbitrarily restricted?
| >>> funlist = [
| ...     (lambda value:
| ...         lambda:'My value is %s'%value
| ...             # imagine freedom to have full suites here
| ...     )(y) for y in range(5)
| ... ]
| >>> for fun in funlist: print fun()

Obviously, you cannot spell 'funlist' quite that way in Python.  But the
available spellings are not bad looking IMO.  E.g., with no lambda:

    >>> def ValFactory(x):
    ...     def say_val(x=x): return 'My value is %s' % x
    ...     return say_val
    ...
    >>> funlist = map(ValFactory, range(5))

I'm not sure the point here.  My spelling happens to be Python's (or at
least one option in Python)... and it works fine without any lambdas.
If you want, you can even 'del' the name 'ValFactory' after the list is
created.

Yours, David...

--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/






More information about the Python-list mailing list