Re: A small programming problem



Richard Harter wrote:
On Wed, 04 Oct 2006 00:52:43 GMT, "John W. Krahn"
<someone@xxxxxxxxxxx> wrote:

Richard Harter wrote:
[snip problem statement]
$ perl -le'
$a0 = [ "b0", "b1" ];
$s1 = [ $a0, "a1" ];
$S = [ "s0", $s1, "s2" ];

$P = do {
my $previous;
sub {
my $list = shift;
for my $item ( @$list ) {
$P->( $item ), next if ref $item eq "ARRAY";
$item .= $previous;
$previous = $item;
}
}
};

[snip data dumper]

If I follow this correctly (and I doubt it, perl is not one of my
languages) you're doing the same thing that mensanator did with the
python solution - you're neglecting the 'state' issue. An explanation
in words would be helpful.

The variable $P is a closure[1,2] and the previous 'state' is stored in the
$previous variable which is lexically scoped so that it is only available to
the closure. HTH



1. http://en.wikipedia.org/wiki/Closure_(computer_science)
2. http://faq.perl.org/perlfaq7.html#What_s_a_closure_


John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.