| Author |
Message |
Tony Tanzillo
Guest
|
Posted:
Sat Jan 01, 2005 8:01 pm Post subject:
Re: a zero length list? |
|
|
"Tom Smith" <nospam> wrote
| Quote: | Doug, now I'm getting confused. First off, I've got a terrifying feeling
that Tony may be wrong on a point, which would upset my whole world view.
Command: (nth 1 nil)
; error: bad argument type: consp nil
Fine, there's no second element of a zero-length list.
Command: (nth 9 '(0 1 2 3))
nil
Oops! There's no 9th element of a 4-element list, but there's no error
either! The error only happens when the list is empty.
|
The error should happen in any case where the index
is not valid (IMO). It doesn't work that way, but it
should. Try using just about any other language that
supports vector type data structures with indexed access
methods, and you'll see that an error is always triggered
when an invalid index is used.
The point being that a result of NIL, can mean one of
two things: The value of the list element at the specified
index is NIL, or the specified index is invalid. This kind of
behavior is what leads to buggy code, and it means you
must include your own runtime test using (length) if you
want robust code that properly fails when an index is
invalid.
The most notorious and difficult to find bugs in LISP, are
often a result of LISP's non-declarative, typeless nature.
For example, if you define a function that expects an
integer argument, and don't check to ensure that the
argument passed to it really is an integer, then the bug
that results can manifest miles away from the defective
code. In a declarative language, where you must declare
the types of arguments and variables, a bug like that
will never see the light of day (e.g., the compiler will
detect it).
Perhaps that's why grown-up, industrial strength LISP
implementations like Common LISP, support variable
and argument type declarations and integral type
checking.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com
|
|
| Back to top |
|
 |
Doug Broad
Guest
|
Posted:
Sat Jan 01, 2005 8:03 pm Post subject:
Re: a zero length list? |
|
|
Tony,
Yes I knew all that as I certainly hope you know by now.
My point was that car/cdr derivatives return different values
than nth (when the intention is to access particular elements)and
that that should be considered when designing a particular
application. For instance, when 2d and 3d point lists are allowed
and the application needs to determine the z coordinate, it would
usually be unfortunate if it errored out rather than using an
assumed (current z) elevation if that were acceptable.
;Code examples
(setq z (cond ((caddr p))(0.0))) ;OK for 2d and 3d points.
(setq z (cond ((nth 2 p)) (0.0))) ;Problematic if 2d points permitted.
(setq z (cond ((= (length p) 3) (nth 2 p)) (0.0)));Seems silly just to avoid error.
There are many similar such examples that I could cite where
default values could be allowed and for which derivatives of cdr/car
may be tedious to implement.
Certainly if I had a fixed length array in another language I would
expect that to fail if I tried to access an element past the end. To do
otherwise would risk memory leaks... But LISP lists can be variable in
length and the variability of the length can be used logically in many ways.
"Tony Tanzillo" <tony.tanzillo@U_KNOW_WHERE.com> wrote in message news:41d69a8d_3@newsprd01...
| Quote: | Sorry, but I fail to see your point. Nil _is_ the value
of the CDR of the last element of every list with the
exception of 'dotted lists' (simply because every normal
list is terminated by nil - while dotted lists are terminated
by the last element).
When you create a list, you can do it using either of
the following two basic semantic conventions, the
first and most common one being 'short-hand' for the
second:
(setq mylist '(1 2 3))
(setq mylist '(1 2 3 . NIL))
So in reality (and what the PRINT function hides
from you), is the real composition of a normal or
'non-dotted' list:
(1 2 3 . NIL)
Which (print) displays as simply
(1 2 3)
Given that, I fail to see any relevance between the
use of NIL to terminate a list (the result of CDR), and
what NTH should do when its given an invalid index.
I don't see any point to debating a fundamental and
widely accepted principles of software engineering and
language/api design, such as this question. In that
sense, you would find yourself in the minority, especially
when you consider that it isn't a language-specific issue.
So, I see no legitimate reason for NTH to return anything
but an error if its given an invalid index. I also see no
relevance between that and the result of CDR or its
derivatives.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com
|
|
|
| Back to top |
|
 |
Tony Tanzillo
Guest
|
Posted:
Sat Jan 01, 2005 8:46 pm Post subject:
Re: a zero length list? |
|
|
| Quote: | Tony,
Yes I knew all that as I certainly hope you know by now.
|
Having it both ways is not an option. Either the
indexed accessor is going to fail with an invalid
index, or its not. What you are saying is that by
not failing, there is some usefulness to that.
What I am saying is that by not failing, there is
a problem with that, in most cases where a LIST
is being used like an array.
Since there are alternative ways of testing a
list whose length is not predictable or where the
number of elements is not assumed, they should
be used for that case, and NTH should be used
for the case where there is an assumption about
the length of the list, and so it should fail if an
invalid index is given.
The decision to use NTH from the outset should
take into consideration the intent that is conveyed
by its use (e.g., it implies that the LIST is used like
an array/vector, not as a list of arbitrary length).
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com
|
|
| Back to top |
|
 |
Matt Stachoni
Guest
|
Posted:
Mon Jan 03, 2005 11:15 pm Post subject:
Re: a zero length list? |
|
|
On Fri, 31 Dec 2004 13:10:38 -0500, "Tom Smith" <nospam> wrote:
| Quote: | That's just the weirdness of nil.
|
I was really questioning the statement
| Quote: | (setq mylist (list))
Defines 'mylist' as type 'list', with nothing in it..Length 0
|
Where mylist is not of type LIST.
Matt
mstachoni@comcast.net
mstachoni@bhhtait.com |
|
| Back to top |
|
 |
|
|
|
|