in reply to Lexical closures
Python must behave this way, no per loop scope. But it let me ask myself (and the monks in fact) why the following code does 024?my $i; $i = 0; push(@flist, sub {$i * $_[0]}); $i = 1; push(@flist, sub {$i * $_[0]}); $i = 2; push(@flist, sub {$i * $_[0]});
Does not my define the scope and then putting it before the loop should not make it apply on the loop? Note: lisp can behave both ways, and maybe even more, one may not compare lisp with javascript:)my $i; foreach $i (0 .. 2) { push(@flist, sub {$i * $_[0]}); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Lexical closures
by GrandFather (Saint) on Oct 25, 2008 at 08:57 UTC | |
by backstab (Novice) on Oct 25, 2008 at 11:08 UTC | |
by chromatic (Archbishop) on Oct 25, 2008 at 18:40 UTC | |
by backstab (Novice) on Oct 25, 2008 at 21:37 UTC | |
by ikegami (Patriarch) on Oct 25, 2008 at 22:33 UTC | |
by GrandFather (Saint) on Oct 25, 2008 at 19:48 UTC | |
by ig (Vicar) on Oct 26, 2008 at 01:37 UTC | |
by backstab (Novice) on Oct 25, 2008 at 20:45 UTC | |
|
Re^2: Lexical closures
by spurperl (Priest) on Oct 25, 2008 at 19:44 UTC | |
by GrandFather (Saint) on Oct 25, 2008 at 20:51 UTC | |
by JavaFan (Canon) on Oct 25, 2008 at 23:03 UTC |