Anregung:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
use strict;
use warnings;
package Range;
sub new {
my $class = shift;
my $lower = shift;
my $upper = shift;
my $delta = shift || 0.00001;
my $self = {
lower => $lower,
upper => $upper,
delta => $delta,
current => $lower,
};
return bless $self, $class;
}
sub next {
my $self = shift;
if ($self->{current} + $self->{delta} < $self->{upper}) {
$self->{current} += $self->{delta};
return 1;
} else {
return undef;
}
}
package main;
sub integrate {
my $function = shift;
my $iter = shift;
my $sum;
do {
$sum += $function->($iter->{current}) * $iter->{delta};
} while ($iter->next());
return $sum;
}
print integrate(sub {(shift)**2}, new Range (2, 5)), "\n";
EDIT: Mit etwas Objektorientierung wird es einfach schöner.
EDIT2: Struktur etwas geändert.\n\n
<!--EDIT|Ronnie|1135278703-->