Python syntax in Lisp and Scheme

David Mertz mertz at gnosis.cx
Wed Oct 15 23:46:19 EDT 2003


> the alpha and omega of HOFs is that functions are first class
> objects that can be passed and returned.

Pascal Bourguignon <spam at thalassa.informatimago.com> wrote previously:
|So, to be really a first class object, a function should let itself be
|modified too:
|[44]> (setq f (function (lambda (x) (+ 1 x))))
|#<CLOSURE :LAMBDA (X) (+ 1 X)>
|Oops, an opaque type, I can't change the body.
|[46]> (setq f (lambda (x) (+ 1 x)))
|#<CLOSURE :LAMBDA (X) (+ 1 X)>

FWIW, you can change the body in Python:

    >>> def foo(x):
    ...     x += 3
    ...     x *= 2
    ...     return x
    ...
    >>> foo(4)
    14
    >>> def bar(y):
    ...     y -= 4
    ...     return y
    ...
    >>> foo.func_code = bar.func_code
    >>> foo(4)
    0
    >>> print foo
    <function foo at 0xbf9cc>


But this is a completely silly thing to do.  The only real meaning of my
comment that Haskell and Python are better for HOFs than is Lisp is that
I find the expression and use of HOFs more "natural"--largely because of
a friendlier syntax.  In the Haskell case--but not Python--the uniform
use of currying makes working with arbitrary level functions
particularly easy.

For this point, I don't really care if some folks retort that Lisp
syntax is actually more natural, or easier to work with.  Fine, I don't
care; let's accept the stipulation.  But the underlying issue was the
spurious claim that lambda forms were somehow required for HOFs, which
is totally bogus.  Python could get by with only 'def', Lisp with only
'defun', and Haskell only '=' and 'where'... and all of them would be
just as capable at using combinatorial and other HOFs.

My little mention spurred this other subthread about things to name and
not name, which repeated some of the same silliness.  Basically, the
truth is that you do not NEED lambda for anything.  Moreover, it is far
more common to use lambdas where aesthetics and readability would be
better without them than it is to use names where lambdas would be
better.  But I am certainly NOT one of the people who advocates taking
lambda out of Python (or out of Haskell or Lisp either, for that
matter--especially since Haskell's '\' symbol is so elegant)--in fact,
in Python circles I am pretty much "Mr. Functional Programming" (Dr.,
actually), try google.  And my book uses way more lambdas than probably
any other Python book... but it's not because I think they're necessary,
or even relevant, to HOFs.

Yours, David...

--
---[ to our friends at TLAs (spread the word) ]--------------------------
Iran nuclear neocon POTUS patriot Pakistan weaponized uranium invasion UN
smallpox Gitmo Castro Tikrit armed revolution Carnivore al-Qaeda sarin
---[ Gnosis Software ("We know stuff") <mertz at gnosis.cx> ]---------------






More information about the Python-list mailing list