differentiate cons & list

Discussion of Emacs Lisp

differentiate cons & list

Postby 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
dericbytes
 
Posts: 1
Joined: Fri Dec 07, 2012 11:48 am

Re: differentiate cons & list

Postby 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).
User avatar
Goheeca
 
Posts: 198
Joined: Thu May 10, 2012 12:54 pm


Return to Emacs Lisp

Who is online

Users browsing this forum: No registered users and 1 guest