Whoa! Do Python and Lisp really have LAMBDA ?

mike420 at ziplip.com mike420 at ziplip.com
Sun Oct 26 02:11:05 EST 2003


Earlier Ed Schofield (thanks, man) warned us that

flist = []

for i in range(3)
    f = lambda x: x + i
    flist.append(f)

[f(1) for f in flist]

gives [3, 3, 3]. So much for the principle of minimum surprise!

Doing the same in Lisp (with lists instead of arrays),

(setf flist (loop for i from 0 to 2 
                  collect (lambda (x) (+ x i))))

(loop for f in flist 
      collect (funcall f 1))

I got (4 4 4).

Lisp has many gotchas, I just wasn't ready for this one.
(Google for "lisp gotchas" - someone posted a comprehensive
list to c.l.l. in 1995. Every Lisper should read it)

I'm sure Haskell does this right. What about Scheme and ML?





More information about the Python-list mailing list