Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between listp and consp?

446 views
Skip to first unread message

rando...@gmail.com

unread,
Mar 2, 2005, 11:14:10 PM3/2/05
to
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 :|

Peter Seibel

unread,
Mar 2, 2005, 11:20:07 PM3/2/05
to
The difference is nothing. Or rather, NIL. That is:

(consp nil) ==> nil
(listp nil) ==> t

-Peter

--
Peter Seibel pe...@gigamonkeys.com

Lisp is the red pill. -- John Fraser, comp.lang.lisp

Joerg Hoehle

unread,
Mar 3, 2005, 4:40:00 AM3/3/05
to
rando...@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

rando...@gmail.com

unread,
Mar 3, 2005, 6:54:36 AM3/3/05
to
hehe, thanks alot for the detailed explanation :D i'll be careful when
i use consp/listp from now on:D thanks alot again !

lin8080

unread,
Mar 3, 2005, 5:19:09 PM3/3/05
to

rando...@gmail.com schrieb:

> hehe, thanks alot for the detailed explanation :D i'll be careful when
> i use consp/listp from now on:D thanks alot again !

ups ...

MACROP

is that true?

stefan

0 new messages