From ... Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed1.bredband.com!bredband!uio.no!nntp.uio.no!ifi.uio.no!not-for-mail From: Erik Naggum Newsgroups: comp.lang.lisp Subject: Re: (endp lst) or (null lst) Date: 04 Jan 2003 09:45:17 +0000 Organization: Naggum Software, Oslo, Norway Lines: 45 Message-ID: <3250662317762335@naggum.no> References: Reply-To: http://naggum.no/erik/contact.html Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: maud.ifi.uio.no 1041673518 8255 129.240.65.207 (4 Jan 2003 09:45:18 GMT) X-Complaints-To: abuse@ifi.uio.no NNTP-Posting-Date: 4 Jan 2003 09:45:18 GMT Mail-Copies-To: never User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: archiver1.google.com comp.lang.lisp:49848 * Chris Gehlker | Comes Stephen Slade to say that the canonical way to test for the | end of proper list lst is (endp lst) rather than (null lst). The | HyperSpec seems to agree. And yet I have the impression that | almost no one uses endp. Almost nobody uses dotted lists to begin with, so why should they think of using `endp´? Give a function that traverses a list naïvely and tests with `null´ a dotted lists and it will most probably stumble on the `cdr´ that gets a non-cons argument at the end of the list. In all likelihood the program will enter the debugger or terminate ungracefully. A much more robust way is not to test for the end of the list at all, but to test for a cons cell that would require more traversal. Take the infinitely silly Scheme-like example of computing `length´ with a maximum of gratuitous overhead: (defun length (list) (if (cons list) (1+ (length (cdr list))) 0)) compared to the seemingly very similar: (defun length (list) (if (null list) 0 (1+ (length (cdr list))))) The cdr chain of a proper list is certainly terminated by an atom that is `nil´, but unless you explicitly attach significance to the specific atom that is the cdr of the last cons, should you care so much as to signal an error if it is not the canonical `nil´ or are you no worse off if you simply ignore it? It is sloppy to pass anything non-`nil´ to `cdr´; careful programming demands that you know that you pass `cdr´ a cons cell. -- Erik Naggum, Oslo, Norway Act from reason, and failure makes you rethink and study harder. Act from faith, and failure makes you blame someone and push harder.