a zero length list?
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
a zero length list?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CADForums.net Forum Index -> Customization
Author Message
hutch
Guest





Posted: Wed Dec 29, 2004 2:58 am    Post subject: a zero length list? Reply with quote

for some functions a nil works okay for a zero lengh list.
but what other way can I create/initialize a zero length list?

Back to top
Tom Smith
Guest





Posted: Wed Dec 29, 2004 3:07 am    Post subject: Re: a zero length list? Reply with quote

Quote:
for some functions a nil works okay for a zero lengh list

They are the same.

Command: (listp nil)
T

Nil is a list.

Command: (eq nil '())
T
Command: (eq nil (list))
T

It's the same as an empty list. You can denote an empty list more explicitly
by quoting it or by using the list function with no arguments, as shown.

It would be interesting to see an example where nil doesn't seem to serve to
describe an empty list.
Back to top
Tony Tanzillo
Guest





Posted: Wed Dec 29, 2004 3:19 am    Post subject: Re: a zero length list? Reply with quote

There is no other way. NIL represents the
empty list, exclusively.

You create/initialize an empty list every time
you use a variable, because every variable's
initial value is NIL.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com

"hutch" <nospam@address.withheld> wrote in message news:8916087.1104271166455.JavaMail.jive@jiveforum2.autodesk.com...
Quote:
for some functions a nil works okay for a zero lengh list.
but what other way can I create/initialize a zero length list?


Back to top
hutch
Guest





Posted: Wed Dec 29, 2004 3:44 am    Post subject: Re: a zero length list? Reply with quote

when you run (nth 1 mylistl) you get an error if mylist is nil.
.... now I suppose I could have tested to see if the list was nil first but that is not the point.
Back to top
Tony Tanzillo
Guest





Posted: Wed Dec 29, 2004 6:22 am    Post subject: Re: a zero length list? Reply with quote

"hutch" <nospam@address.withheld> wrote

Quote:
when you run (nth 1 mylistl) you get an error if mylist is nil.

Which is exactly what it should do. You are asking for
the 2nd element in a list of 0 elements. That's not much
different than asking for the 9th element in a 4 element
list. Either case, should result in an error.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com
Back to top
Tom Smith
Guest





Posted: Wed Dec 29, 2004 9:13 am    Post subject: Re: a zero length list? Reply with quote

If mylist might be empty, then your program needs to cover that eventuality, along the lines of

(if mylist
(do-something-with mylist)
)

which will return nil if mylist is empty.
Back to top
ECCAD
Guest





Posted: Wed Dec 29, 2004 9:43 pm    Post subject: Re: a zero length list? Reply with quote

How about:
(setq mylist (list))
Defines 'mylist' as type 'list', with nothing in it..Length 0

Yo.

Bob
Back to top
Tom Smith
Guest





Posted: Wed Dec 29, 2004 10:03 pm    Post subject: Re: a zero length list? Reply with quote

Quote:
(setq mylist (list))

Bob, I mentioned that earlier. To my eye, (list) without arguments isn't
quite as clear as a quoted empty list '(), but they're the same thing.
Tony's point is interesting. Any new variable at all, if it hasn't been
bound to a value, evaluates to nil and therefore is a list.

Command: (eval newvar)
nil
Command: (listp newvar)
T
Command: (length newvar)
0

So if you want newvar to be a list, you don't have to do anything at all,
just start working with it as such.

Command: (setq newvar (append newvar '(a b c)))
(A B C)
Back to top
Matt Stachoni
Guest





Posted: Fri Dec 31, 2004 9:10 pm    Post subject: Re: a zero length list? Reply with quote

On Wed, 29 Dec 2004 16:43:50 GMT, ECCAD <nospam@address.withheld> wrote:

Quote:
Defines 'mylist' as type 'list', with nothing in it..Length 0

??

(setq mylist (list)) => nil
(type mylist) => nil

Matt
mstachoni@comcast.net
mstachoni@bhhtait.com
Back to top
Doug Broad
Guest





Posted: Fri Dec 31, 2004 10:11 pm    Post subject: Re: a zero length list? Reply with quote

Tony and all,

Though we take the nth behavior as a given, and must program
accordingly, I would hardly say nth's evaluation is consistent with
other functions that return similar values.

Examples: (cadr nil) -> nil
(caddr nil) -> nil

Commonly, the cadr of the list is taken to mean the second
element of the list. To be consistent, it would seem that
nth should return a similar value (not an error).

Nil is commonly used to represent false. Depending on the application
nil returned as the nth value of a list could indicate non-existance of
that element without stopping the program.

A more accomodating version of nth could be appropriate for
a particular application:

;;A less testy form that allows the the ndx to be
;;greater than the length of the list
(defun snth (ndx lst) ;DCB
(cond
((or
(/= (type ndx) 'int)
(atom lst)
(< ndx (length lst)))
(nth ndx lst)) ;;cause an appropriate error if necessary.
(t nil))) ;;ndx is past end of list.


Happy New Year.


"Tony Tanzillo" <tony.tanzillo@U_KNOW_WHERE.com> wrote in message news:41d206d9$1_1@newsprd01...
Quote:
"hutch" <nospam@address.withheld> wrote

when you run (nth 1 mylistl) you get an error if mylist is nil.

Which is exactly what it should do. You are asking for
the 2nd element in a list of 0 elements. That's not much
different than asking for the 9th element in a 4 element
list. Either case, should result in an error.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005
http://www.acadxtabs.com

Back to top
Tom Smith
Guest





Posted: Fri Dec 31, 2004 11:01 pm    Post subject: Re: a zero length list? Reply with 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. In fact, I can't see
a difference in behavior between nth and your snth. They both error on an
empty list, and return nil if the index is too great.

Second, I had thought that the presumed error behavior on too high an index
actually made some sense. For instance, a list might contain nil as an
element.

Command: (nth 0 '(t nil))
T
Command: (nth 1 '(t nil))
nil
Command: (nth 2 '(t nil))
nil

The return value is ambiguous here, I think -- you can't distinguish between
nil actually being the second element, and the index being too high. To
determine whether a certain element exists and really is nil, you'd need to
always check the length too. Your thoughts?
Back to top
Tom Smith
Guest





Posted: Fri Dec 31, 2004 11:10 pm    Post subject: Re: a zero length list? Reply with quote

Quote:
(setq mylist (list)) => nil
(type mylist) => nil

That's just the weirdness of nil.

(listp nil) => T
(atom nil) => T
(type nil) => nil

Nil doesn't have a type. Bob's (setq mylist (list)) does define mylist as a
list, but an empty list is nil and has no type. And as Tony impied, the setq
wasn't necessary anyway -- the inital value of mylist would have been the
same nil anyway.
Back to top
Doug Broad
Guest





Posted: Fri Dec 31, 2004 11:58 pm    Post subject: Re: a zero length list? Reply with quote

Tom,

Good testing. How about this one?
(nil is both an atom and a list)

;;Another form that allows the the ndx to be
;;greater than the length of the list
(defun snth2 (ndx lst)
(cond
((or
(/= (type ndx) 'int)
(not (listp lst))
(< ndx (length lst)))
(nth ndx lst)) ;;cause an appropriate error if necessary.
(t nil))) ;;ndx is past end of list.

The issues of whether a certain result is appropriate (nil or error)
depend more on the application than a general statement.

In most cases, it might be appropriate to error out when
the index is too high. There might possibly be applications where
returning nil versus error might be better.

I agree that one drawback of allowing a nil return for an item out
of range could easily lead to ambiguity for the case where nil elements
are allowed.






"Tom Smith" <nospam> wrote in message news:41d593e0$1_1@newsprd01...
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. In fact, I can't see
a difference in behavior between nth and your snth. They both error on an
empty list, and return nil if the index is too great.

Second, I had thought that the presumed error behavior on too high an index
actually made some sense. For instance, a list might contain nil as an
element.

Command: (nth 0 '(t nil))
T
Command: (nth 1 '(t nil))
nil
Command: (nth 2 '(t nil))
nil

The return value is ambiguous here, I think -- you can't distinguish between
nil actually being the second element, and the index being too high. To
determine whether a certain element exists and really is nil, you'd need to
always check the length too. Your thoughts?

Back to top
Tom Smith
Guest





Posted: Sat Jan 01, 2005 12:26 am    Post subject: Re: a zero length list? Reply with quote

Quote:
Good testing. How about this one?
(nil is both an atom and a list)

Yeah, I pointed that out to Matt. And weirder still (or maybe as a result of
that), an empty list does not have a type.

(type '()) => nil ( It ain't nothin')

Atom is an odd duck in AutoLISP anyway, I've always thought. It doesn't have
the traditional p suffix like other predicates, and AFAIK it's not any
different from (not (listp <item>)), so it seems a bit redundant.

I'll have to cogitate on the various nth issues. I can't recall when I've
had problems with its return values. Generally when I've dealt with big
lists I've done it recursively, or using a foreach, or else used an
association list where the order didn't matter.
Back to top
Tony Tanzillo
Guest





Posted: Sat Jan 01, 2005 5:41 pm    Post subject: Re: a zero length list? Reply with quote

"Doug Broad" <dbroad@earthlink.net> wrote
Quote:
Tony and all,

Though we take the nth behavior as a given, and must program
accordingly, I would hardly say nth's evaluation is consistent with
other functions that return similar values.

Examples: (cadr nil) -> nil
(caddr nil) -> nil

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
 
Post new topic   Reply to topic    CADForums.net Forum Index -> Customization All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Powered by phpBB