Something is rotten in Denmark...

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Jun 2 08:51:15 EDT 2011


Alain Ketterlin writes:
> Steven D'Aprano writes:
> > I agree it's not intuitive. But where does it say that programming
> > language semantics must always be intuitive?
> 
> Nowhere. But going against generally accepted semantics should at
> least be clearly indicated. Lambda is one of the oldest computing
> abstraction, and they are at the core of any functional programming
> language. Adding a quick hack to python and call it "lambda" is just
> abuse of terminology (I don't say python is the only abuser,
> implementing lambda is difficult; see, e.g., Apple C extension
> called "blocks" and their implementation of binding).

As far as I can see, and that may not be far enough, Python's lambda
expressions do implement a generally accepted semantics. It seems to
be essentially the same semantics that Scheme uses. The value of a
lambda expression is closed in the environment where it is evaluated.
When called, a function looks up the values of its free variables in
the environment where it is closed. (*)

Alonzo Church in his lambda calculus did not deal with variable
assignment at all, as far as I know - he was a logician, not a
programming language designer - and so he also did not need to talk
about _when_ things happen.

Purely functional programming languages also do not have variable
assignment and the whole issue of this thread simply cannot arise.

(*) Some Python folks insist that Python does not have variables or
variable assignment. They talk of names and rebinding of names. And
they call environments namespaces. But the important point is the
underlying reality:

    >>> k=1; f=lambda : k; k=2; f()
    2

Or the same with a closed-over variable that is only accessible
through the values of the lambda expressions at the time they are
called:

    >>> def g(n):
    ...    f1=lambda : n
    ...    n += 1
    ...    f2=lambda : n
    ...    n += 1
    ...    return f1, f2
    ... 
    >>> f,h = g(1)
    >>> f(), h()
    (3, 3)

As expected.

Too long, didn't read? A Schemer finds no fault with Python's lambda.



More information about the Python-list mailing list