Lisp - consp listp

Discussion of Common Lisp

Lisp - consp listp

Postby phdenis40 » Thu Jun 23, 2016 1:38 pm

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.
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: Lisp - consp listp

Postby pjstirling » Thu Jun 23, 2016 9:23 pm

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).
pjstirling
 
Posts: 136
Joined: Sun Nov 28, 2010 4:21 pm

Re: Lisp - consp listp

Postby pjstirling » Thu Jun 23, 2016 9:30 pm

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))))
pjstirling
 
Posts: 136
Joined: Sun Nov 28, 2010 4:21 pm


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron