Thanks... sorry for the stupid mistake. Here's the corrected code:
#!/usr/bin/perl -w
# summation examples from SICP done in Perl
use strict;
use warnings;
my $result = &sum_integers(1, 3);
print "sum_integers(1, 3) equals $result\n";
$result = &cube(3);
print "cube(3) equals $result\n";
$result = &sum_cubes(1, 3);
print "sum_cubes(1, 3) equals $result\n";
$result = &sum_cubes2(1, 3);
print "sum_cubes2(1, 3) equals $result\n";
sub sum_integers {
my ($a, $b) = @_;
if ($a > $b) { return(0); }
else { return( $a + &sum_integers(($a + 1), $b) ); }
}
sub cube {
my ($a) = @_;
return( $a * $a * $a );
}
sub sum_cubes {
my ($a, $b) = @_;
if ($a > $b) { return(0); }
else { return( &cube($a) + &sum_cubes(($a + 1), $b) ); }
}
sub sum {
my ($term, $a, $next, $b) = @_;
if ($a > $b) { return(0); }
else { return( $term->($a) + &sum( $term, ($next->($a)), $next, $b
+) ); }
}
sub inc {
my ($n) = @_;
return( $n + 1 );
}
sub sum_cubes2 {
my ($a, $b) = @_;
return( &sum(\&cube, $a, \&inc, $b) );
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.