Skip to main content

char=, char/=, char<, char>, char<=, char>=, char-equal, char-not-equal, char-lessp, char-greaterp, char-not-greaterp, char-not-lessp

char=, char/=, char*<, char>, char<=, char>*=, . . .

char=, char/=, char < , char > , char < =, char > =, char-equal, char-not-equal, char-lessp, char greaterp, char-not-greaterp, char-not-lessp Function

Syntax:

char= &rest characters+ → generalized-boolean

char*/* = &rest characters+ → generalized-boolean

char< &rest characters+ → generalized-boolean

char> &rest characters+ → generalized-boolean

char<= &rest characters+ → generalized-boolean

char>= &rest characters+ → generalized-boolean

char-equal &rest characters+ → generalized-boolean

char-not-equal &rest characters+ → generalized-boolean

char-lessp &rest characters+ → generalized-boolean

char-greaterp &rest characters+ → generalized-boolean

char-not-greaterp &rest characters+ → generalized-boolean

char-not-lessp &rest characters+ → generalized-boolean

Arguments and Values:

character—a character .

generalized-boolean—a generalized boolean.

Description:

These predicates compare characters.

char= returns true if all characters are the same; otherwise, it returns false. If two characters differ in any implementation-defined attributes, then they are not char=.

char/= returns true if all characters are different; otherwise, it returns false.

char< returns true if the characters are monotonically increasing; otherwise, it returns false. If two characters have identical implementation-defined attributes, then their ordering by char< is consistent with the numerical ordering by the predicate < on their codes.

char> returns true if the characters are monotonically decreasing; otherwise, it returns false. If two characters have identical implementation-defined attributes, then their ordering by char> is consistent with the numerical ordering by the predicate > on their codes.

char<= returns true if the characters are monotonically nondecreasing; otherwise, it returns false. If two characters have identical implementation-defined attributes, then their ordering by char<= is consistent with the numerical ordering by the predicate <= on their codes.

char>= returns true if the characters are monotonically nonincreasing; otherwise, it returns false.

char=, char/=, char*<, char>, char<=, char>*=, . . .

If two characters have identical implementation-defined attributes, then their ordering by char>= is consistent with the numerical ordering by the predicate >= on their codes.

char-equal, char-not-equal, char-lessp, char-greaterp, char-not-greaterp, and char-not-lessp are similar to char=, char/=, char<, char>, char<=, char>=, respectively, except that they ignore differences in case and might have an implementation-defined behavior for non-simple characters. For example, an implementation might define that char-equal, etc. ignore certain implementation-defined attributes. The effect, if any, of each implementation-defined attribute upon these functions must be specified as part of the definition of that attribute.

Examples:

(char= #\d #\d) → true 
(char= #\A #\a) → false
(char= #\d #\x) → false
(char= #\d #\D) → false
(char/= #\d #\d) → false
(char/= #\d #\x) → true
(char/= #\d #\D) → true
(char= #\d #\d #\d #\d) → true
(char/= #\d #\d #\d #\d) → false
(char= #\d #\d #\x #\d) → false
(char/= #\d #\d #\x #\d) → false
(char= #\d #\y #\x #\c) → false
(char/= #\d #\y #\x #\c) → true
(char= #\d #\c #\d) → false
(char/= #\d #\c #\d) → false
(char< #\d #\x) → true
(char<= #\d #\x) → true
(char< #\d #\d) → false
(char<= #\d #\d) → true
(char< #\a #\e #\y #\z) → true
(char<= #\a #\e #\y #\z) → true
(char< #\a #\e #\e #\y) → false
(char<= #\a #\e #\e #\y) → true
(char> #\e #\d) → true
(char>= #\e #\d) → true
(char> #\d #\c #\b #\a) → true
(char>= #\d #\c #\b #\a) → true
(char> #\d #\d #\c #\a) → false
(char>= #\d #\d #\c #\a) → true
(char> #\e #\d #\b #\c #\a) → false
(char>= #\e #\d #\b #\c #\a) → false
(char> #\z #\A) → implementation-dependent
(char> #\Z #\a) → implementation-dependent
(char-equal #\A #\a) → true

(stable-sort (list #\b #\A #\B #\a #\c #\C) #’char-lessp)
(#\A #\a #\b #\B #\c #\C)
(stable-sort (list #\b #\A #\B #\a #\c #\C) #’char<)
(#\A #\B #\C #\a #\b #\c) ;Implementation A
(#\a #\b #\c #\A #\B #\C) ;Implementation B
(#\a #\A #\b #\B #\c #\C) ;Implementation C
(#\A #\a #\B #\b #\C #\c) ;Implementation D
(#\A #\B #\a #\b #\C #\c) ;Implementation E

Exceptional Situations:

Should signal an error of type program-error if at least one character is not supplied.

See Also:

Section 2.1 (Character Syntax), Section 13.1.10 (Documentation of Implementation-Defined Scripts)

Notes:

If characters differ in their code attribute or any implementation-defined attribute, they are considered to be different by char=.

There is no requirement that (eq c1 c2) be true merely because (char= c1 c2) is true. While eq can distinguish two characters that char= does not, it is distinguishing them not as characters, but in some sense on the basis of a lower level implementation characteristic. If (eq c1 c2) is true, then (char= c1 c2) is also true. eql and equal compare characters in the same way that char= does.

The manner in which case is used by char-equal, char-not-equal, char-lessp, char-greaterp, char-not-greaterp, and char-not-lessp implies an ordering for standard characters such that A=a, B=b, and so on, up to Z=z, and furthermore either 9<A or Z<0.

Expanded Reference: char=, char/=, char<, char>, char<=, char>=, char-equal, char-not-equal, char-lessp, char-greaterp, char-not-greaterp, char-not-lessp

Case-sensitive equality with char= and char/=

char= tests whether characters are identical (case-sensitive). char/= tests whether all characters are pairwise different.

(char= #\a #\a)
=> T
(char= #\a #\A)
=> NIL
(char= #\a #\b)
=> NIL
(char/= #\a #\b)
=> T
(char/= #\a #\a)
=> NIL

Ordering with char<, char>, char<=, char>=

These test monotonic ordering of characters. Uppercase letters are ordered A through Z, lowercase a through z, and digits 0 through 9. The relative order of uppercase vs. lowercase is implementation-dependent.

(char< #\a #\b #\c)
=> T
(char< #\a #\b #\b)
=> NIL
(char<= #\a #\b #\b)
=> T
(char> #\z #\m #\a)
=> T
(char>= #\z #\z #\a)
=> T
(char< #\0 #\1 #\9)
=> T

Multiple argument comparisons

All these functions accept one or more characters. With multiple arguments, they check that the ordering relation holds between every consecutive pair (or every pair for char/=).

(char= #\x #\x #\x #\x)
=> T
(char= #\x #\x #\y #\x)
=> NIL
(char/= #\a #\b #\c)
=> T
(char/= #\a #\b #\a)
=> NIL

Case-insensitive comparison with char-equal and friends

char-equal, char-not-equal, char-lessp, char-greaterp, char-not-greaterp, and char-not-lessp work like their case-sensitive counterparts but ignore case differences.

(char-equal #\A #\a)
=> T
(char-not-equal #\A #\a)
=> NIL
(char-lessp #\a #\B)
=> T
(char-greaterp #\Z #\a)
=> T
(char-not-greaterp #\a #\A)
=> T
(char-not-lessp #\Z #\z)
=> T

Sorting characters

The case-insensitive comparison functions are useful for sorting characters without regard to case.

(sort (list #\C #\a #\B #\d) #'char-lessp)
=> (#\a #\B #\C #\d)

(sort (list #\C #\a #\B #\d) #'char<)
;; => implementation-dependent ordering of upper/lowercase

Practical use: case-insensitive string character comparison

(defun char-equal-at (s1 s2 index)
"Compare characters at the same index in two strings, ignoring case."
(char-equal (char s1 index) (char s2 index)))

(char-equal-at "Hello" "HELLO" 0)
=> T
(char-equal-at "Hello" "World" 0)
=> NIL