Page 1 of 1

Lisp - consp listp

Posted: Thu Jun 23, 2016 1:38 pm
by phdenis40
Hello all,

I'm trying to find a way in order to detect the difference between these 2 forms:

Code: Select all

(setq MyLst '(word1 word2))
(cons 'word1 'word2)
I've tried to use the consp macro or the listp macro in order to be able to highlight these 2 differents forms. Unfortunatelly, the result is always the same T
I think I missed something.

Re: Lisp - consp listp

Posted: Thu Jun 23, 2016 9:23 pm
by pjstirling
Well, it depends on how you are doing things, in general.

They aren't EQUAL, because the first creates 2 cons cells, and the second only 1 (it will print as a dotted list).

Re: Lisp - consp listp

Posted: Thu Jun 23, 2016 9:30 pm
by pjstirling
Hmm, you seem to misunderstand, CONSP and LISTP a) aren't macros b) only differ over the handling of NIL (CONSP returns false for NIL, LISTP returns true)

This is because LISTP doesn't check for proper lists, but only whether its argument is a CONS or NIL

If you want to do something special for dotted pairs, then test

Code: Select all

(and (cdr x)
     (not (consp (cdr x))))