Of what use is 'lambda'???

Gareth McCaughan Gareth.McCaughan at pobox.com
Sun Sep 24 21:22:57 EDT 2000


[I said:]
>> In Scheme, you can use the exact same syntax internally as
>> you use at top level. (Up to R4RS, this was an optional
>> part of the language, though almost every implementation
>> supported it. As of R5RS it's mandatory.)
> 
> Unfortunately, although you can use the same syntax, it doesn't always
> work the same.  In SCM, this code works at the top level, but breaks
> inside a function:
> 
> (define (adder x) (lambda (y) (+ x y)))
> (define adder5 (adder 5))
> (define adder6 (adder 6))
> (adder5 (adder6 x))
..
> I don't understand R5RS well enough to know whether this code is
> supposed to work properly or not.

It's not. A bunch of internal defines is equivalent to
a "letrec". The right-hand sides are evaluated before
any of the names are bound. In particular, the calls
(adder 5) and (adder 6) try to make use of the definition
of "adder", which they're not allowed to do.

Another triumph of elegance over practicality in Scheme,
I think. :-)

> >Of course, there's also a difference in *utility* between
> >Python and (modern) Lisps, in that their lambdas make
> >lexical closures and Python's don't.
> 
> Well, and any function can be a lambda in Lisp, while only expressions
> can be lambdas in Python.  Reminds me of DEF FN.  :)

Yes indeed. The fundamental problem is that Guido doesn't
really like "lambda" and its friends, I think. I'm not
quite sure why he put them into the language when he
doesn't like them...

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construc



More information about the Python-list mailing list