Whoa! Do Python and Lisp really have LAMBDA ?
Marcin 'Qrczak' Kowalczyk
qrczak at knm.org.pl
Sun Oct 26 11:08:12 CET 2003
On Sun, 26 Oct 2003 00:11:05 -0700, mike420 wrote:
> I'm sure Haskell does this right. What about Scheme and ML?
Indeed Haskell does this right.
OCaml does this right.
SML doesn't have a for loop. If you emulate it with recursion idiomatic
to SML (passing the incremented argument, not using a mutable reference)
then it will work.
Scheme doesn't have a for loop either, I think it's like in SML - or would
it be more idiomatic to use "set!"? in which case it would not work.
Ruby does this wrong if you use "for i in 0..2 do ... end" but right if
you use "(0..2).each do |i| ... end".
Smalltalk does this right, unless you use some ancient implementations
which make block parameters local to the method in which they are written.
I'm not sure how widespread are such implementations.
Perl does this right if you remember to use "foreach my $i (...)" instead
of "foreach $i (...)" or "foreach (...)". In the latter cases a global
variable is used which is obviously wrong. I think Perl courses should
emphasize "my" more.
In Java I think you can't reference a mutable variable from a local class
but you can reference a final variable, so it detects the problem and
requires manual creation of an immutable binding to work around it.
I suspect that the newer C# which will have anonymous functions does this
What about Dylan? Erlang? Mercury?
Moral 1: first class functions are better used with functional style
(immutable data). It's because they make the time when something is
evaluated harder to see, which is fine as long as data is immutable.
In this example it's easy to see that the lambda is evaluated later
but it's not as easy to notice that it matters that the dereferencing of
the variable happens when the function is called, not when it's created.
By taking away the possibility of mutation you take away some surprises.
Moral 2: if you design a language with closures, it's better not to use
a shared mutable variable in a "for" loop.
__("< Marcin Kowalczyk
\__/ qrczak at knm.org.pl
More information about the Python-list