Elisp: Boolean (true false nil)
Boolean in Emacs Lisp
There is no Boolean Type in elisp.
nil
, 'nil
,
and empty list (list )
are false, anything else is true.
nil
is a builtin variable. Value ofnil
is Symbol'nil
.t
is a builtin variable. Value oft
is Symbol't
. It is used to represent true by convention.
;; all false (if nil "true" "false") (if 'nil "true" "false") (if () "true" "false") (if '() "true" "false") (if (list) "true" "false")
By convention, t
is used for true.
;; all true (if t "true" "false") (if 0 "true" "false") (if "" "true" "false") (if (vector ) "true" "false")
Boolean Functions
and
(and t nil) ; nil ;; can take multiple args (and t nil t t t t) ; nil
or
(or t nil) ; t
not
(not t) ; nil (not nil) ; t (not 2) ; nil
Function Name Ending in p
Function names that end with letter “p” often means it returns boolean (i.e. nil
or not nil
).
(The “p” stands for “predicate”).
examples:
boundp
buffer-modified-p
consp
fboundp
featurep
file-directory-p
file-exists-p
file-readable-p
functionp
integerp
listp
numberp
stringp
symbolp
vectorp
zerop