Of what use is 'lambda'???

Kragen Sitaker kragen at dnaco.net
Thu Sep 21 19:47:00 EDT 2000


In article <slrn8skp55.lp.Gareth.McCaughan at g.local>,
Gareth McCaughan <Gareth.McCaughan at pobox.com> wrote:
>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))

It says:

unbound variable:  adder
; in expression: (... adder 5)
; in scope:
;   (adder adder5 adder6 . #@define)
;   (x)
;Evaluation took 0 mSec (0 in gc) 49 cells work, 5 env, 213 bytes other

It works fine in Guile, though.

This little surprise made some of my code a little uglier than I would
have liked; I had lots of stuff in the top-level for easy interactive
play, and then I tried to wrap it in a local scope once it was debugged
so it wouldn't pollute the namespace.

I don't understand R5RS well enough to know whether this code is
supposed to work properly or not.

>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.  :)

-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we ourselves
possess.
                -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"]



More information about the Python-list mailing list