Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Passing subroutines as arguments

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


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

Interesting... So what's wrong with this? sum_cubes2(1, 3) is returning 1 instead of 36 like it should and giving the warning "Use of uninitialized value in numeric gt (>) at /Users/mvaline/bin/summation line 35."

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


Comment on Re: Re: Passing subroutines as arguments
Download Code
Re:^3 Passing subroutines as arguments
by arturo (Vicar) on May 08, 2003 at 20:24 UTC

    Read perldiag for an explanation of the error message; the error message does not lie, you're attempting to compare values, at least one of which is undefined. There's only one comparison in the code you post, so assuming that's the "offending" line, the problem is that one of $a and $b is not initialized in the call to sum. Why that would be so depends on how that subroutine came to be called, and if you look at the else clause of your conditional in the sum subroutine, you'll see the problem; what you pass to the call to sum there are three things: $term, and the results of calling the sub it points to on $a and $b. Yet your subroutine's ... ummm... signature calls for there to be four objects, so the result is that when that recursive call is made, $b is undefined.

    HTH

    If not P, what? Q maybe?
    "Sidney Morgenbesser"

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://256663]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-11-24 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (137 votes), past polls