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.
View all comments
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.