Elisp: Equality Test

By Xah Lee. Date: . Last updated: .

Generic Equality Tests

equal

Check datatype and value are same.

This is the most generic and commonly used. Can be used for comparing

  • Two Symbols
  • Two Strings
  • Two Integers
  • Two Floats
  • Two List's contents

🛑 WARNING: comparing a integer number and floating number doesn't work. (equal 3 3.0) returns nil.

;; test if two values have the same datatype and value.

(equal 3 3) ;  t
(equal 3.0 3.0) ;  t

(equal 3 3.0) ;  nil. Because datatype doesn't match.

;; test equality of lists
(equal '(3 4 5) '(3 4 5))  ;  t
(equal '(3 4 5) '(3 4 "5")) ;  nil

;; test equality of strings
(equal "e" "e") ;  t

;; test equality of symbols
(equal 'abc 'abc) ;  t
eq

Check if is the same object. Good for comparing

;; work on symbols
(eq 'x 'x) ; t

;; work on integer
(eq 2 2) ; t

;; ------------

;; does not work for string
(eq "e" "e") ; nil

;; does not work for float
(eq 2.1 2.1) ; nil

;; does not work on lists having same items
(let ( aa bb )
  (setq aa '(3 4))
  (setq bb '(3 4))
  (eq aa bb))
;; nil

;; work on lists having same address
(let ( aa bb )
  (setq aa '(3 4))
  (setq bb aa)
  (eq aa bb))
;; t
eql

like eq but also return true for two “same” floating numbers with same sign.

(eql 1.1 1.1) ; t
(eql 0.0 -0.0) ; nil

Compare Numbers

=

Compare String, Symbol

string-equal and others.

Compare Char

char-equal and others.

Test Inequality

;; use “not” to invert equality
(not (equal 3 4)) ;  t

Reference

Elisp, Boolean