It seems to me that the list terminator in Lisp could be any arbitrary value. For example, the string terminator in C is the null pointer. Is there a philosophical reason why the empty list was chosen to terminate lists?
migrated from stackoverflow.com Apr 16 '12 at 15:12
|
A Lisp list is not really terminated with an empty list -- it's terminated with a special value, traditionally called
An empty list is basically just a rather short-hand way of expressing NIL -- since you don't even have a single cons cell, all you're left with is the NIL that terminates the list. In other words, it's not that a list is terminated with an empty list -- it's that an empty list consists of only a terminator. Using dot notation, it's possible to link cons cells together in other ways, but the result isn't a "list" as the term is normally used. |
|||||||||||||
|
|
Lisp lists don't really have a 'terminating' value. Actually Lisp does not even have a primitive list data structure. Lists are built out of the empty list and a chain of linked cons cells pointing to list contents. Lists are recursively defined:
A list is empty when it is the empty list or Because In older times Historically (and still in Common Lisp which aims to be backwards compatible), the empty list is also the unique symbol
|
|||
|
|
|
I will give you the philosophical answer to your philosophical question: It seems to me that the list terminator in Lisp could be any arbitrary value. [...] Is there a philosophical reason why the empty list was chosen to terminate lists? The answer to your question is this: the list terminator is an arbitrary value. It is so arbitrary that different Lisp dialects use a different value for it and yet remain conceptually compatible. Common Lisp uses a symbol object: the symbol (Except that when we work with mathematical objects with pencil and paper, we use symbols. A circle with a dash through it or {} represent an empty set, etc. So why not use machine symbols similarly?) Scheme uses an empty list, the object The Scheme So therefore, is there a philosophical reason why the empty list terminates lists? Yes! The reason is that any object which is understood to terminate a list is, by definition, the empty list. If you use the number 42 as the list terminator, then Mathematically, every list has the empty list as its suffix (just like every set has the empty set as a subset). Since the Lisp list representation is suffix-based (a non-empty list is an reference to an object holding the first element, and the suffix of that list), there has to be something to handle the case when a list has just one element, and its proper suffix is (mathematically) the empty list. Whatever object is installed as the suffix is then understood to be the empty list, and for the sake of consistency and unity, that object better be used as the representation of the stand-alone empty list also, not only as the representation of the empty suffix of the one-element list. |
||||
|
|
|
This may be not language specific and somewhat anti-scientific explanation, but may give you some thoughts. You could think about the language as a set of all facts pertaining to the "world" or "universe" created by that language. As this is a set, you would need to have a complement set for all facts excluded from the language, i.e. something to mean "not in this set". In certain light |
|||
|
|
|
As Rainer and Jerry explained, Lisp lists are built from cons cells so it's not really and empty list. Conceptually it's often helpful to think this way when defining recursive functions though. It's a bit more obvious if we look at List data type definition in (for example) Haskell.
What this says is that either we have a
Which in Lisp could be
So conceptually thinking of a list as being terminated by the empty list can be very helpful, |
|||
|
|


0is a null pointer constant, but pointers are not numbers. – Keith Thompson Apr 16 '12 at 19:33cdrcell -- but the resulting object is then known as an "improper list". This is arguably a philosophical distinction, but it has practical consequences, since an extra value must be handled differently from regular list elements. – comingstorm Apr 16 '12 at 22:36