Keep It Simple, Stupid PerlMonks

### Re: Re:^3 Passing subroutines as arguments

by mvaline (Friar)
 on May 08, 2003 at 20:37 UTC ( #256673=note: print w/replies, xml ) Need Help??

in reply to Re:^3 Passing subroutines as arguments
in thread Passing subroutines as arguments

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) );
}

Create A New User
Node Status?
node history
Node Type: note [id://256673]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-07-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (416 votes). Check out past polls.

Notices?