Writing a counter function

Shlomi Fish shlomif@vipe.technion.ac.il
Sat, 29 Jun 2002 20:47:03 +0300 (IDT)


Just for the record, here is a Perl function that does this:

###############
sub counter
{
    my $a = shift;

    my $next = sub {
        my $to_add = shift ;
        return counter($to_add+$a);
    };

    return ($a, $next);
}

my ($result,$next) = counter(5);
my ($result2, $next2) = $next->(100);
my ($result3, $next3) = $next2->(50);
my ($result4, $next4) = $next->(30);

print "\$result=$result\n\$result2=$result2\n\$result3=$result3\n\$result4=$result4\n";
############

Regards,

	Shlomi Fish



----------------------------------------------------------------------
Shlomi Fish        shlomif@vipe.technion.ac.il
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       shlomif@iglu.org.il

He who re-invents the wheel, understands much better how a wheel works.