Here we describe functions that test for equality between two objects. Other functions test equality of contents between objects of specific types, e.g., strings. For these predicates, see the appropriate chapter describing the data type.
This function returns
tif object1 and object2 are the same object, and
If object1 and object2 are integers with the same value, they are considered to be the same object (i.e.,
t). If object1 and object2 are symbols with the same name, they are normally the same object—but see Creating Symbols for exceptions. For other types (e.g., lists, vectors, strings), two arguments with the same contents or elements are not necessarily
eqto each other: they are
eqonly if they are the same object, meaning that a change in the contents of one will be reflected by the same change in the contents of the other.(eq 'foo 'foo) ⇒ t (eq 456 456) ⇒ t (eq "asdf" "asdf") ⇒ nil (eq "" "") ⇒ t ;; This exception occurs because Emacs Lisp ;; makes just one multibyte empty string, to save space. (eq '(1 (2 (3))) '(1 (2 (3)))) ⇒ nil (setq foo '(1 (2 (3)))) ⇒ (1 (2 (3))) (eq foo foo) ⇒ t (eq foo '(1 (2 (3)))) ⇒ nil (eq [(1 2) 3] [(1 2) 3]) ⇒ nil (eq (point-marker) (point-marker)) ⇒ nil
make-symbolfunction returns an uninterned symbol, distinct from the symbol that is used if you write the name in a Lisp expression. Distinct symbols with the same name are not
eq. See Creating Symbols.(eq (make-symbol "foo") 'foo) ⇒ nil
The Emacs Lisp byte compiler may collapse identical literal objects, such as literal strings, into references to the same object, with the effect that the byte-compiled code will compare such objects as
eq, while the interpreted version of the same code will not. Therefore, your code should never rely on objects with the same literal contents being either
eq, it should instead use functions that compare object contents such as
equal, described below. Similarly, your code should not modify literal objects (e.g., put text properties on literal strings), since doing that might affect other literal objects of the same contents, if the byte compiler collapses them.
This function returns
tif object1 and object2 have equal components, and
eqtests if its arguments are the same object,
equallooks inside nonidentical arguments to see if their elements or contents are the same. So, if two objects are
eq, they are
equal, but the converse is not always true.(equal 'foo 'foo) ⇒ t (equal 456 456) ⇒ t (equal "asdf" "asdf") ⇒ t (eq "asdf" "asdf") ⇒ nil (equal '(1 (2 (3))) '(1 (2 (3)))) ⇒ t (eq '(1 (2 (3))) '(1 (2 (3)))) ⇒ nil (equal [(1 2) 3] [(1 2) 3]) ⇒ t (eq [(1 2) 3] [(1 2) 3]) ⇒ nil (equal (point-marker) (point-marker)) ⇒ t (eq (point-marker) (point-marker)) ⇒ nil
Comparison of strings is case-sensitive, but does not take account of text properties—it compares only the characters in the strings. See Text Properties. Use
equal-including-propertiesto also compare text properties. For technical reasons, a unibyte string and a multibyte string are
equalif and only if they contain the same sequence of character codes and all these codes are in the range 0 through 127 (ASCII).(equal "asdf" "ASDF") ⇒ nil
However, two distinct buffers are never considered
equal, even if their textual contents are the same.
The test for equality is implemented recursively; for example, given
two cons cells x and y,
(equal x y
t if and only if both the expressions below return
(equal (car x) (car y)) (equal (cdr x) (cdr y))
Because of this recursive method, circular lists may therefore cause infinite recursion (leading to an error).
This function behaves like
equalin all cases but also requires that for two strings to be equal, they have the same text properties.(equal "asdf" (propertize "asdf" 'asdf t)) ⇒ t (equal-including-properties "asdf" (propertize "asdf" 'asdf t)) ⇒ nil