Python syntax in Lisp and Scheme

Bengt Richter bokr at oz.net
Fri Oct 17 07:18:48 EDT 2003


On Thu, 16 Oct 2003 23:10:52 -0400, mertz at gnosis.cx (David Mertz) wrote:

>|>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
You snipped the line following the preceding:
(unless you create some specialized syntax for a recursive call)

>
>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
I'd call that a specialized syntax, to serve my purposes ;-)

>
>|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.
That's what I meant by the first line in
"""
Practically, it is probably rare that you can't use a named function,
but really def is a convenience construct that does name binding after
doing (what would be) a full-fledged lambda thing IMO.
"""
>
>|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
The above spelling works fine, being verbatim cut/paste). Just not if you want
to replace the comment as it invites you to imagine. I guess the hypothetical
version is what you were referring to by 'that way.'

>available spellings are not bad looking IMO.  E.g., with no lambda:
I pre-agreed, following my example with:
"""
(Not that a bound method (aka callable object if bound method is __call__) might
not be as good or better in many cases, ...
"""
>
>    >>> 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.

The relevant phrase from the post you are replying to was
"... where the def names would at best be ignored side effects." (Although
you are forced to use the temp bindings to return the function).

>If you want, you can even 'del' the name 'ValFactory' after the list is
>created.
Sure. Interesting you chose to use the default-value hack rather than
a closure ;-). What I had in mind when I pre-agreed was something like

 >>> class SayVal(object):
 ...     def __init__(self, v): self.v=v
 ...     def __call__(self): return 'My value is %s' % self.v
 ...
 >>> funlist = map(SayVal, range(5))
 >>> for f in funlist: print f()
 ...
 My value is 0
 My value is 1
 My value is 2
 My value is 3
 My value is 4

Regards,
Bengt Richter




More information about the Python-list mailing list