[comp.lang.lisp] list-length vs. listp in Common Lisp?

ruffwork@orstcs.CS.ORST.EDU (Ritchey Ruff) (09/06/88)

Anyone notice how hard it is to make sure you pass a "true"
list to list-length (or length)?  I know CLtL says that
(LISTP <X>) will not tell a doted-list from a true-list,
but then if you test for a list with listp and it returns "t"
you still may get an error from "list-length" saying "arg not a list"
(boy, did this ever make a lot of sense when I first had it happen
  to me ;-).  The only way I've found to make sure an arg is a true
list is with -
	(defun true-listp (l)
	       (and (listp sexp)
		    (null (cdr (last l)))))
but then it's easier to write my own version of list-length
and add an extra termination condition for dotted endings.
(I want to know how many data items are in the list so that I
can precess them, so (1 2 3 4 5 . 6) is length 6 and
(1 2 3 4 5) is length 5, etc.) 

it would be nice if you could do something like
	(list-length sexp :doted-ending-allowed)
Isn't there an easier way?  (now watch, even though I just spent over
an hour pouring through Steele somebody will say, "Oh, that's
the function called true-listp on page xyz of CLtL...;-).

--ritchey ruff	ruffwork@cs.orst.edu -or- ...!tektronix!orstcs!ruffwork
	"I haven't lost my mind, it's backed up on tape somewhere..."

Krulwich-Bruce@cs.yale.edu (Bruce Krulwich) (09/07/88)

In article <6317@orstcs.CS.ORST.EDU>, ruffwork@orstcs (Ritchey Ruff) writes:
>Anyone notice how hard it is to make sure you pass a "true"
>list to list-length (or length)?   The only way I've found to make sure an 
>arg is a true list is with -
>	(defun true-listp (l)
>	       (and (listp sexp)
>		    (null (cdr (last l)))))
...
>it would be nice if you could do something like
>	(list-length sexp :doted-ending-allowed)
>Isn't there an easier way?  (now watch, even though I just spent over
>an hour pouring through Steele somebody will say, "Oh, that's
>the function called true-listp on page xyz of CLtL...;-).

Well, regardless of whether TRUE-LISTP is built in or user-written, it will
still be slower to do this because the list will end up traversed twice.  The
only way to avoid this inefficiency is to write your own LIST-LENGTH that has
options to deal with a dotted ending (or to try to get such options added to
the CL standard).

Bruce