Re: Recursion
Re: Recursion
- Subject: Re: Recursion
- From: Dave Stewart <email@hidden>
- Date: Fri, 26 Sep 2003 11:28:38 -0700
On Friday, September 26, 2003, at 10:12 AM, Rich Carroll wrote:
>
>
Kai you beautiful Applescript madman.
>
This works perfect. I didn't know that Applescript would finish any
>
earlier
>
iterations. This is part of a much larger script, so that's why it may
>
look
>
disconnected. I appreciate all your "picky" comments. I obviously am
>
not a
>
pro at this, so those points help me to understand the language a bit
>
better.
</perpetual lurking>
That's how recursion works, the call is made to the
handler/function/method and when that returns the remaining statements
are executed. I can't think of any language that does anything
different, can anyone else?
Rich, run this and see if it clarifies things for you:
----------start tested script snippet----------
recursionTest(5)
on recursionTest(recursionControlVariable)
--The first time through this handler we check to see if the variable
passed in
--is greater than one. It should be, so we'll recursively call this
handler again
--subtracting one from the variable. After a few times of doing this,
we'll
--pass this test (our variable will equal 1) and we'll jump to the
next statement.
if (recursionControlVariable > 1) then --base case :
(recursionControlVariable <= 1)
--Recursive call - make sure it subracts one each time or this
--will be an infinite loop! First rule of recursion - make sure you
can
--attain a "base case" with your recursive calls!
recursionTest(recursionControlVariable - 1)
end if
--This statement is first executed when recursionControlVariable
--equals 1, then again each time a recursive call finishes. Notice that
--the variable is decremented in each recursive call, yet the dialogs
--count up from 1 to whatever we first call this with (5 in this case)!
display dialog "Number equals " & recursionControlVariable
end recursionTest
----------end tested script snippet----------
As a general rule of thumb, when I need something recursive I tend to
write recursive routines that do something only when the "base case" is
achieved and I try not to do anything after a recursive call (example,
the display dialog above). This generally helps reduce confusion. The
usual disclaimers apply (exceptions to every rule, generalized
statements are all false including this one, etc).
Hope this has helped!
<humor moment>
PS: I was having fun with the "Ovulation Test" posts! All work and no
play bores everyone to death, right? Let me see if I got this right,
condoms encourage auto accidents in the same way seat belts encourage
sex, right? That explains some of the close calls I've had in my
stupider (I mean younger) days ...
;-)
I swear this list has the best sense of humor sometimes ...
</humor moment>
<perpetual lurking>
Dave Stewart
Aqua-flo Supply (Goleta)
email@hidden
A computer lets you make more mistakes faster than any invention in
human history with the possible exceptions of handguns and tequila.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.