differentiate cons & list

Discussion of Emacs Lisp
Post Reply
dericbytes
Posts: 1
Joined: Fri Dec 07, 2012 11:48 am

differentiate cons & list

Post by dericbytes » Fri Dec 07, 2012 11:53 am

I want to know when an item is a cons and when it is a list

Code: Select all

(consp (cons 1 1))
> t

Code: Select all

(listp (cons 1 1))
>t

Code: Select all

(consp (list 1 1))
>t

Code: Select all

(listp (list 1 1))
>t

Code: Select all

(nth 1 (cons 1 1))
> error not a list

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: differentiate cons & list

Post by Goheeca » Sat Dec 08, 2012 4:15 am

What about this:

Code: Select all

(defun true-list-p (list)
  (cond ((null list) t)
        ((consp list) (true-list-p (cdr list)))
        (t nil)))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Post Reply