hi, i have tried and failed, to test out the difference between consp
and listp. I mean practical reasons.. apparently the only difference
recorded in hyperspec is that consp test if a object is cons, and listp
test if an object is a list :|.. they are the same to me for all
practical reason :|
The difference is nothing. Or rather, NIL. That is:
(consp nil) ==> nil
(listp nil) ==> t
-Peter
--
Peter Seibel ·····@gigamonkeys.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
··········@gmail.com writes:
> hi, i have tried and failed, to test out the difference between consp
> and listp.
Too me, there is a difference in style.
I often see code like
(if (listp foo) (do-something-with (car foo)))
which I rewrite in most cases to
(if (consp foo) (do-something-with (car foo)))
because I prefer to minimize *superflous* use of (car NIL)
-- I'm not a Schemer, I know when to take advantage of (car NIL)
Mind you, it's not equivalent code. It depends on whether
(do-something-with NIL) is wished for or not.
Often enough, it's a superfluous call that'll do nothing.
For example, in the Iterate package, I found 85% of listp occurrences
could be turned into consp. Now consp occurs 31 times, and listp 5
times only.
I guess listp is used much more often than the more precise consp
because some people like to think of lists (cf. Kent Pitman's recent
comment on CAR/CDR, LHS/RHS, FIRST/REST).
Here's an example where I very much prefer to use consp over listp:
(when (and (consp clause) (eq (car clause) 'declare))
(dolist (spec (cdr clause)) ...))
BTW, for the cycle-counter, consp is likely to compile to a single
machine instruction, whereas listp adds an OR test for the NIL case.
Regards,
Jorg Hohle
Telekom/T-Systems Technology Center