Wouldn't this work? Not that I know off the top of my head, but I've
used similar structures in the past (not for heavy calculation work
mind you, mostly for lexical parsing loops...)
our $answer = recursive_function (initial parameter list);
sub recursive_function {
my $answer;
some stuff to do;
do we have answer?
if yes
return $answer;
else
$answer = recursive_function (refined parameter list);
return $answer;
end
}
On Thu, May 21, 2009 at 4:20 PM, Greg Aiken <[email protected]> wrote:
> our $global_answer;
>
>
>
> recursive_function (initial parameter list);
>
>
>
> sub recursive_function {
>
>
>
> some stuff to do;
>
> do we have answer?
>
> if yes {
>
> $main::global_answer = ...
>
> return;
>
> else
>
> recursive_function (refined parameter list);
>
> return;
>
> end
>
> }
>
>
>
>
>
>
>
> ________________________________
>
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Greg
> Aiken
> Sent: Thursday, May 21, 2009 2:02 PM
> To: [email protected]
> Subject: question about recursion
>
>
>
> given:
>
> a. that its generally considered to be ‘bad form’ to use global variables
> inside of sub-routines.
>
> b. that I need to write a recursive sub-routine to solve a mathematical
> problem.
>
>
>
> the sub-routine will recursively call itself until the ‘answer’ is derived.
> when the innermost call finishes executing, the program will drop through to
> the previous call, and so forth… until it drops out of the original first
> call to the function.
>
>
>
> at present, I am ‘cheating’ here by using a global variable to control this
> recursive behavior. when the innermost loop finds the ‘answer’, the global
> variable is set to $weve_found_answer=1;
>
>
>
> the sub-routines then look to the value of $main::weve_found_answer to
> determine if it should stop recursively calling itself because the answer
> has already been found – or continue to recursively call itself again.
>
>
>
> for now it ‘works’ but its not following the ‘don’t use globals unless there
> is no better way’ principle…
>
>
>
> so I ask… ‘is there a better way of allowing each sub routines
> instantiation, at any nested level of the recursive call, be able to know
> when the answer has been found?’
>
>
>
> if anyone has any insights or experience with this, I would be very
> interested to learn from your experience
>
>
>
> thanks, greg
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
>
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs