Re: recursion depth
Re: recursion depth
- Subject: Re: recursion depth
- From: has <email@hidden>
- Date: Tue, 11 Oct 2005 22:59:33 +0100
Barry Wainwright wrote:
>Here's a practical demonstration of recursion:
>
>on commafy(theNum)
> if theNum > 999 then return (commafy(theNum div 1000) & "," & text -3 ¬
>thru -1 of ("00" & ((theNum mod 1000) as text)))
> return theNum as text
>end commafy
Wouldn't be my choice as that's the sort of task that's as easily done with iteration, and since novices will likely find an iteration-based version easier to think about and understand they won't see any point to using recursion in its place. Recursion's a non-trivial concept for newbies to wrap their heads around, so you have to sell it on its biggest advantages if you want them to bother doing so.
The sorts of things that really demonstrate the value of recursion are crawling tree structures (e.g. mapping filesystem hierarchies) and divide-and-conquer algorithms (binary search, quicksort). Using recursion here is much simpler and a lot more natural than using iteration. For example (wishing AS had a decent 'print'-style command instead of that lame 'log'):
on listFolders(theFolder, indent)
tell application "Finder" to set subFolders to every folder of theFolder
repeat with subFolder in subFolders
log (indent & subFolder's name)
listFolders(subFolder, indent & tab)
end repeat
end listFolders
listFolders(choose folder, "")
This has the advantage of relating to a naturally recursive structure that scripters are already very familiar with, so it's easier for them to make sense of what it does and why. I always hesitate to use the word 'intuitive', but I think crawling the filesystem is as intuitive a demonstration of recursion as you can get.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden