Re: How can I STOP a cursed recursion?
Dan Moyer wrote:
>
> Igor Kachan wrote:
> >
> > Dan Moyer wrote:
> > >
> > >
> > > With some difficulty, I built a recursive procedure, which seems to work
> > > fine, except that I can't get it to STOP.
> > >
> > > It knows when it gets to where it should stop, & I put return in at that
> > > point, & when that didn't work, I set an external variable to 1
> > > (=finished)
> > > when it got to that point, and added a test at the beginning of the
> > > procedure
> > > to "return" if it saw that var "finished", but that didn't work either.
> > >
> > > The procedure was to take a sequence of sequences like what "folder" would
> > > have when adding items to a TreeView, and go thru them to compute the
> > > correct
> > > positions to put those items into in a "horizontal" treeView. The
> > > procedure
> > > worked thru the items in a correct fashion, got to the last item just
> > > below
> > > the root item, knew it was where it should stop, but continues to maybe
> > > "unravel" previous left-over recursions, or something?????
> > >
> > > Is there some way to just STOP a procedure, even if it has been working
> > > recursivly?? Or maybe recursion is supposed to be limited to FUNCTIONS?
> > >
> > >
> > > procedure Help!()
> > > print(1,"help!")
> > > Help{}
> > > -- How can I stop!?
> > > end procedure
> > >
> > > Help!
> > >
> > > Dan
> >
> >
> > Hi Dan,
> >
> > Try please:
> > }}}
<eucode>
> > procedure Help()
> > if get_key()!=-1 then return end if
> > print(1,"help!")
> > Help()
> > -- How can I stop!?
> > end procedure
> > Help()
> > </eucode>
{{{
> >
> > Regards,
> > Igor Kachan
> > kinz at peterlink.ru
>
> Thanks Igor, but I want the *procedure* to stop itself when it sees by test
> that it should be finished, not a user action stop it.
>
> eg:
> }}}
<eucode>
> procedure Help()
> print(1,"help!")
> Help()
> if getTime() = 6am then
> STOP! -- How can I let the procedure *stop itself*!?
> end if
>
> end procedure
> Help()
> </eucode>
{{{
>
> Dan
Hi Dan.
In any case, to stop a recursive procedure you must test its ending *before*
calling it. If not, it will recurse forever, until it eats all available
storage.
Typical example (code not tested)
function factorial (atom x)
if x = 0 then
return 1
end if
return factorial(x - 1) * x
end function
|
Not Categorized, Please Help
|
|