Re: Re: Generic Length (was: Closures versus objects)

Robbert Haarman <[email protected]>
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
On Tue, Feb 20, 2007 at 11:02:03AM -0600, [email protected] wrote:
> 
>     The problem with the example of length is, I think, specific to 
> length, but interesting in its own right.  There are two distinct and 
> separate ideas of length at play here, which we might call 'collection 
> length' and 'string length' (or more general versions of these two 
> ideas). All objects that can contain other objects (including 
> themselves, for example) have a well-defined length, including 
> sometimes infinity, when they form a cycle.  String length delves into 
> the inner structure of a special-purpose data type.  For example, one 
> could define 'integer length' as the number of bits or digits in an 
> int, and 'byte length' as the amount of memory used by an object, but 
> these are not the same category as the length of a list. 
> List/collection-length wise, it is appropriate to ask the length of a 
> string, but the answer will always be zero.  With this definition the 
> problem can be seen as conflating two operations into a single 
> generic.

Actually, no. A string is an array of characters. As such, it is a 
collection with length equal to the number of characters in it.

The problem with the length functions that have been given is not that 
there is a fundamental issue with different types of length, but simply 
that these functions are buggy.

>     But what about the times you want to iterate over the "well known"
> interpretation of the number of elements in an object, and want to use
> one generic to do it?  This could still work, if you recognize that
> the *initial* dispatch based on type selects the *kind* of length to
> apply recursively to the rest of the object.  Then once you have
> started looking at an object in terms of collection-length, it would
> be a logic error to switch to string-length for the tail of the
> object.  Thus a single generic would give well-defined answers for
> proper lists, strings, and improper lists ending in strings.

That is exactly how a generic length should behave, with the caveat you 
added:

>     Note that it may not be immediately intuitive what the answer is in
> the last case though

Note that length as defined in Common Lisp works on proper sequences. 
So:

(length (list 1 2 3)) => 3
(length "foo") => 3
(length '(1 2 . "foo")) => error: not a proper list

Regards,

Bob

-- 
Only after the last tree has
been cut down

Only after the last river has
been poisoned

Only after the last fish has
been caught

Only then will you find that
money cannot be eaten.

(Cree Indian Prophecy)
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF2zJ7fb9wcmD+WN4RAgkQAJ4loc6TE2R39fmmmEN2jmW8sJFISwCgtQgu
vBR4h/EhLQVe+ArdcPA9ttc=
=hxdy
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.