[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: LIST-LENGTH & circular lists
From: Olin Shivers <shivers@xxxxxxxxxxxxxxxxxx>
>But defining the length of an infinite list to be 0 is not such a good
>think. The length of () is 0. The length of "foo" is 0. The length of a
>circular list ain't 0! It's infinite. The notion of "length" is connected
>the number of times you can apply CDR to the value, which is 0 times for ()
>and unbounded for a circular list. So you need some kind of infinity
>marker/value. (If this were J, we'd just have it return infinity.) We
>draft #F to serve as the infinity value, which has the charm of being easy
>detect using conditionals like AND, OR and COND's => clause.
>So, it also seems to me to be just as reasonable to require LENGTH to
>produce a result on all Scheme values *of finite length*. Values not having
>finite length could either cause LENGTH to report an out-of-domain error or
>could simply not define what the function does.
>These two modes of use are why I have suggested we provide both
> (LENGTH x) -> integer (or diverge or report an error)
> (LENGTH+ x) -> integer or #f
>This way, if you are prepared to deal with a possible non-integer value,
>you can consciously write down a call to the extra-special LENGTH+
>Otherwise, you'll never see a non-integer.
>I note that I previously called these things LIST-LENGTH and LIST-LENGTH+,
>that was a brain-o. The R5RS name is LENGTH, not LIST-LENGTH. It is a
>fact of list-lib's design that I have pervasively dropped the type-prefix
>LIST- from all procedures, just as the RnRS's have. The LIST-LENGTH crept
>in because Currie used it in the msg that raised this whole topic, and I
>responded to that, and we were off and running. Let's return to the RnRS
>name roots for now.
Does this proposal imply that (LENGTH "abc") and (LENGTH '#(a b c))
return 0? If it does, it will conflict with probably more 'natural' attempts
generalize LENGTH to accept other kinds of sequences. Calling it
LIST-LENGTH does not affect existing programs and does not introduce
any potential conflicts.
Even with LIST-LENGTH, I still think that the proposed terminology shift
is not justified in general LISP/Scheme context. The fact that
(LIST-LENGTH 5) returns 0 makes you expect (LIST? 5) to return #t
etc. What was wrong with IMPROPER-LIST?