in reply to Re: pathsearch/backtracking in Perl - how to solve a problem apparently requiring it? (comparison of Perl with Standard ML)
in thread pathsearch/backtracking in Perl - how to solve a problem apparently requiring it? (comparison of Perl with Standard ML)

I don't like global vars (even in disguise). So thats my take on yours.
#!/usr/bin/perl use strict; use warnings; my $amount = 16; my $coinset = [5,2]; sub change { my ($cset, $amt) = @_; if ($amt == 0) { return [] } if (@$cset == 0) { return "" } my $coin = shift @$cset; if ($coin > $amt) { print "$coin > $amt\n"; $coin=change($cset, $amt); return$coin if!ref$coin; return[$coin]; } else { print "Checking $amt - $coin\n"; my $rval =change( [$coin, @$cset], $amt - $coin ); return[$coin, @{$rval}]if ref $rval; print "Exception forces backing up to $amt\n"; return [@{change($cset, $amt)}]; } } use Data::Dumper; print Dumper(change($coinset, $amount)), "\n";
  • Comment on Re^2: pathsearch/backtracking in Perl - how to solve a problem apparently requiring it? (comparison of Perl with Standard ML)
  • Download Code

Replies are listed 'Best First'.
Variable vs. special value as exception
by Roy Johnson (Monsignor) on Nov 13, 2004 at 02:10 UTC
    It's not a global, it's a static. What's not to like about it?

    Caution: Contents may have been coded under pressure.