Re: Counting the items in anested list
Re: Counting the items in anested list
- Subject: Re: Counting the items in anested list
- From: garbanzito <email@hidden>
- Date: Sun, 20 May 2001 10:18:16 -0600
at 5/17/01, 11:58 AM -0700, they whom i call Paul Berkowitz wrote:
Steve, Ehsan wasn't trying to count non-list items. He was trying to count
the "basic" lists {"a, "b"}, {"c", "d"}, {"e", "f"} (namely 3) etc.
[...] I managed that with a repeat loop incorporating a
recursive handler. (Check my last post.) See if you can do
away with the repeat loop if you can.
i had missed your other post with the slightly
changed subject, and i obviously scanned the thread too
quickly. but my main point holds, that you don't need
a repeat loop to traverse nested lists.
i think of nested lists (or any lists) as a binary tree,
and a traversal can be done by recursing on the first item
and the rest of the list as two "branches". AppleScript
lacks a command for "rest of list", so i "try" for items 2
through -1.
now that i understand the task at hand, here's how i'd do
it without a repeat loop (incorporating some of Nigel's
nice example):
on bottom_lists(a_list)
local the_count
if class of a_list is not list then
set the_count to 0
else
set the_lists to lists of a_list
if the_lists is {} then
set the_count to 1 -- this is a node that counts
else
set the_count to bottom_lists(first item of the_lists)
try -- if rest of list exists
bottom_lists(items 2 through -1 of the_lists)
set the_count to the_count + result
end try
end if
end if
return the_count
end bottom_lists
steve harley email@hidden