Skip to content

Instantly share code, notes, and snippets.

Created February 13, 2014 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8977597 to your computer and use it in GitHub Desktop.
Save anonymous/8977597 to your computer and use it in GitHub Desktop.
sub leftrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
my $sum = [+] do f($_) for $a, *+$h ... $b-$h; # gets prior def of &f somehow
# my $sum = [+] (f($_) for $a, *+$h ... $b-$h); # works
$h * $sum;
}
sub tryem(&f, $a, $b, $n, $exact) {
say ' exact result: ', $exact;
say ' rectangle method left: ', leftrect &f, $a, $b, $n;
}
tryem { $_ ** 3 }, 0, 1, 100, 0.25;
tryem { 1 / $_ }, 1, 100, 100, log(100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment