Elisp: Proper List
What is Proper List
A
Proper List
is nil
,
or a
Elisp: Cons Cell
such that its last element is nil
,
or its last element is cons
and its last element is nil
,
etc.
;; proper lists ;; because the tail is nil (cons nil nil) (cons 3 nil) (cons 3 (cons 3 nil)) (cons 3 (cons 3 (cons 3 nil))) (cons 3 (cons 3 (cons 3 (cons 3 nil)))) ;; also proper lists. tail is nil (cons (cons 3 4) nil) (cons (cons 3 4) (cons 3 nil)) (cons (cons (cons 3 4) 4) nil) ;; also proper list nil
Improper List
A cons
that's not proper list is called Improper List .
;; improper list (cons nil 3) ;; last element is not nil (cons (cons 3 nil) 3) ;; last element is not nil
Test If a List is a Proper List
proper-list-p
-
Return length if it is a proper list, else
nil
.(proper-list-p nil) ;; true (proper-list-p (cons nil nil)) (proper-list-p (cons 3 nil)) (proper-list-p (cons 3 (cons 3 nil))) (proper-list-p (cons 3 (cons 3 (cons 3 nil)))) (proper-list-p (cons 3 (cons 3 (cons 3 (cons 3 nil))))) ;; all true (proper-list-p (cons (cons 3 4) nil)) ;; true
;; improper list ;; all false ;; tail is not nil (proper-list-p (cons nil 3)) ;; tail is not nil (proper-list-p (cons (cons 3 nil) 3))
Proper List vs Improper List
- Any cons structure, is a list.
- Proper list and improper list are both lists.
Most list functions, assume you have a proper list. That is, they assume you have a nil as the tail in the cons structure. Practically all functions that return a list, return a proper list. This is critical to know.
Improper list is almost never used except a single non-nested cons pair holding 2 values.