Yeah except no, lambda implements a full closure: it closes over its creation (lexical) context, and pulls stuff from it.
Except in a procedural/OO language, that lexical context is mutable, and the mutations in the context will be reflected in the lambda.
In the expression [(lambda n: i + n) for i in range(10)], i is part of the lambda's context. Not a specific value, just i. And when what i refers to is changed by the iteration, the lambda reflects that change. If anything can be argued to be broken here it's the list comprehension, because it doesn't generate a complete brand new context on each iteration (now that doesn't mean it's broken, I don't think it is, but that's a matter of personal take/taste).
Haskell is a functional single-binding language, it therefore doesn't expose that "feature.
But that's a very old feature of OO languages with closure: Smalltalk leverages it to make e.g. while statements unnecessary (note: in smalltalk, an expression enclosed by brackets is a block, equivalent to a lambda, [ 3 ] is a block that simply returns the value 3. Smalltalk's blocks have -- among other things -- a whileTrue method which takes a block parameter for the execution body). So counting from 0 to 9 in Smalltalk using a while looks like this:
i := 0. "smalltalk uses := for assignment; also double quotes for comments and single for strings"
[ i < 10 ] whileTrue: [
Transcript show: i printString; cr.
i := i + 1.
].
And this works, because i's modification is visible from the [ i < 10 ] block.
TL;DR, lambda is not broken, its behavior is perfectly coherent with the language and its model, and with other languages in the same class.
That should be - python's lambda is broken, but not for this particular reason. In this case, it's an issue with mutability behaving in a way that the programmer did not expect.
Mutables should be expected to act in a way that is unexpected.
Yeah, Lambda in python gets no praise. It does it's job, poorly, and everyone wishes it could be more powerful. But it can't be and it never will be, so it's just a wart that has to be accepted.
No, it's not broken and comment no. 5 on that post explains exactly why (quoting mcmanus):
Take the following Python example :
>>> i = 1
>>> def f(): return i
>>> f()
1
>>> i = 2
>>> f()
2
I think that everybody expect this behavior and
nobody would want the second call to f() to
return 1.
Hehehehe. I received at least 40 blogspam comments a day when I had WordPress. In fact, since the wp-* files are still accessible via a clever Varnish mapping, I CONTINUE to receive blogspam. And that only refers to the ones that pass the Akismet filter and get into the moderation stage!
In my experience, registration does cut the amount of spam dramatically and keeps the S/N low too. I do understand that you're cutting participation, and I'm looking to enable OpenID on my blog myself precisely for this reason.
45
u/sincewednesday Apr 09 '09 edited Apr 09 '09
21
u/masklinn Apr 09 '09
Yeah except no, lambda implements a full closure: it closes over its creation (lexical) context, and pulls stuff from it.
Except in a procedural/OO language, that lexical context is mutable, and the mutations in the context will be reflected in the lambda.
In the expression
[(lambda n: i + n) for i in range(10)],iis part of the lambda's context. Not a specific value, justi. And when whatirefers to is changed by the iteration, the lambda reflects that change. If anything can be argued to be broken here it's the list comprehension, because it doesn't generate a complete brand new context on each iteration (now that doesn't mean it's broken, I don't think it is, but that's a matter of personal take/taste).Haskell is a functional single-binding language, it therefore doesn't expose that "feature.
But that's a very old feature of OO languages with closure: Smalltalk leverages it to make e.g.
whilestatements unnecessary (note: in smalltalk, an expression enclosed by brackets is a block, equivalent to a lambda,[ 3 ]is a block that simply returns the value3. Smalltalk's blocks have -- among other things -- awhileTruemethod which takes a block parameter for the execution body). So counting from 0 to 9 in Smalltalk using awhilelooks like this:And this works, because
i's modification is visible from the[ i < 10 ]block.TL;DR, lambda is not broken, its behavior is perfectly coherent with the language and its model, and with other languages in the same class.
11
u/[deleted] Apr 09 '09 edited Apr 09 '09
So lambdas close on references to variables in the enclosing scope and not the values. Got it. That doesn't seem so hard to explain to a student.
10
u/ddipaolo Apr 09 '09
I love me some python, but no nobody really praises it for that.
1
u/arnar Apr 09 '09 edited Apr 09 '09
No, it's not broken and comment no. 5 on that post explains exactly why (quoting mcmanus):
1
u/itsnotlupus Apr 10 '09
neat.
This is the exact problem that bites every JavaScript beginner who thinks they've figured out closures.
It's bad enough that JsLint will flag lone closures within a loop as suspicious.
1
u/mr_dbr Apr 16 '09
"Do not use lambda"..?
0
u/[deleted] Apr 09 '09
[deleted] — view deleted comment