Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ambiguous Depth of a list?! - Help!

4 views
Skip to first unread message

K Herner

unread,
Jan 10, 2002, 8:17:29 PM1/10/02
to
I know by definition that the depth of a list is considered to be the
number of successful cars or cdrs you can make in an S-expression.

So e.g. (depth 'a) = 0

I was given however a definition of the following function that
calculates the average depth of a list:

eg. adepth 'a = 1
(adepth '(a . b)) = 2
(adepth (cons 'a (cons 'b 'c))) = 8 / 3

The / 3 bit is obvious (a, b and c)...But what about the 8 bit? Using
my definition the depth of this expression would be 2 not 8...

Unless they're counting the levels...eg. 'a is at level 1, so b is at
level 3, and c is at level 4 = 8?

Can anyone produce a piece of code that gives these results? Why the
difference in the definition?

Thanks

Jeffrey M. Vinocur

unread,
Jan 11, 2002, 8:56:35 PM1/11/02
to
In article <b469d7f6.02011...@posting.google.com>,

K Herner <roya...@hotmail.com> wrote:
>I know by definition that the depth of a list is considered to be the
>number of successful cars or cdrs you can make in an S-expression.
>
>So e.g. (depth 'a) = 0

I'm not aware of a standard definition for depth. I'd call it
"the length of the longest path when the expression is viewed as
a tree", I guess, but you always have a question about whether to
start at 0 or 1.


>I was given however a definition of the following function that
>calculates the average depth of a list:
>
>eg. adepth 'a = 1
>(adepth '(a . b)) = 2
>(adepth (cons 'a (cons 'b 'c))) = 8 / 3
>
>The / 3 bit is obvious (a, b and c)...But what about the 8 bit? Using
>my definition the depth of this expression would be 2 not 8...

I don't know how you're getting 2.


>Unless they're counting the levels...eg. 'a is at level 1, so b is at
>level 3, and c is at level 4 = 8?

Uh. Look at it like a tree:

(You'll need a fixed-width font such as Courier or Monaco if the
right edge of the below box doesn't line up perfectly.)

+--------------+
| |
| . |
| / \ |
| a /\ |
| b c |
| |
+--------------+

The depth to a is 2, the depth to b is 3, the depth to c is 3.
Average those three numbers.


>Can anyone produce a piece of code that gives these results?

Yes...but you probably can too once you understand the
definition.


--
Jeffrey M. Vinocur * jm...@cornell.edu
http://www.people.cornell.edu/pages/jmv16/

Bob

unread,
Jan 12, 2002, 11:06:22 AM1/12/02
to
exactly, and besides, one can have a car and a cdr with multiple parts,
returning multiple values....

(define car (lambda (X) (values (list-ref (list-ref X 0) 0) (list -ref
(list-ref X 1) 0)))
(define cdr (lambda (X) (values (list-ref (list-ref X 0) 1) (list-ref
(list-ref X 1) 1)))

some kinda structure like ((1 2) (3 4))

here, car and cdr have been redefined to have multiple values (for whatever
reason)
yet it is still possible to ask for the depth of your structure....in other
words, it makes
perfect sense to ask depth of non-pair datastructures


Bob

(define cdr (lambda (X) (values (
"Jeffrey M. Vinocur" <jm...@cornell.edu> wrote in message
news:a1o54j$cbq$1...@marduk.litech.org...

A. Student

unread,
Jan 14, 2002, 2:16:26 PM1/14/02
to
> >
> >So e.g. (depth 'a) = 0
>
> I'm not aware of a standard definition for depth. I'd call it
> "the length of the longest path when the expression is viewed as
> a tree", I guess, but you always have a question about whether to
> start at 0 or 1.

<snip>

> >Can anyone produce a piece of code that gives these results?
>
> Yes...but you probably can too once you understand the
> definition.

It's always nice when one casually peruses newsgroups and notices someone else who's been struggling with the same problems as
you. Even more amusing when said person blatantly tries do disguise the fact that they're looking for answers to the same degree
coursework that you're doing.

I think "K Herner"'s confusion stemmed from the fact that the definition of depth used in his/her/our course starts at 0, ie
(depth 'a) = 0, whereas the example output for the code required used a definition of depth that starts at 1.


0 new messages