Jump to content

Jensen's Device: Difference between revisions

(added perl)
Line 206:
 
print sum(\$i, 1, 100, sub { 1 / $i }), "\n";</perl>
Output: 5.18737751763962
 
Or you can take advantage of the fact that elements of the @_ are aliases of the original:
<perl>my $i;
sub sum {
my (undef, $lo, $hi, $term) = @_;
my $temp = 0;
for ($_[0] = $lo; $_[0] <= $hi; $_[0]++) {
$temp += $term->();
}
return $temp;
}
 
print sum($i, 1, 100, sub { 1 / $i }), "\n";</perl>
Output: 5.18737751763962
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.