klsxqa
Last Updated: February 25, 2016
·
1.467K
· raghuugare

LISP: consp or listp ?

When exploring the built-in predicates, Beginning LISPers (in ANSI Common LISP esp.) will often get the following doubt:

Aren't consp & listp the same?

Short answer : No

Why is that?

Well, because not every list is a cons-cell !

What? Can you please explain?

Sure.
Note that NIL is a list, also denoted by ().

But (most importantly!) NIL is not a cons-cell.


There!

I hope you 'got' the subtle difference between consp & listp predicates in LISP.

It should now be clear what the following code snippets produce (Please do try!) :

(consp NIL)

Output: NIL (=false)

where as,

(listp NIL)

Output: T (=true)


Note: This pro-tip gives a very compact explanation. For a more detailed write-up, please refer to my detailed article here