Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

LISP – Comparison Operators on Characters & Strings

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The contents of a field are compared to the contents of another field or a constant using Comparison operators. In Simple words, comparator operators are used to compare whether the two or more different values are equal or not. 

Comparison Operators on Character:

Characters are not supported by numerical comparison functions and operators such as < and >. Other than that, Two sets of functions in Common LISP are used. The first set is case-sensitive, whereas the second is not case-sensitive or (case-insensitive). 

Following are some comparison functions that may be used to see whether the two characters are equivalent or not equal.

Case-SensitiveCase-InsensitiveExplanation
CHAR= CHAR-EQUALChecks if the operands’ values are all equal; if they are, the condition is true.
CHAR/=CHAR-NOT-EQUALChecks if the operands’ values are all different or not; if they aren’t, the condition is true.
CHAR<CHAR-LESSPIf the character1 is smaller than character2, the condition is true; otherwise, the condition is false.
CHAR<=CHAR-NOT-GREATERPIf any of the left character’s values are less than or equal to the value of the following right character, the condition is true.
CHAR>CHAR-GREATERPIf character1 is greater than character2, the condition is true; otherwise, it is false.
CHAR>=                     CHAR-NOT-LESSP                                  If any left character’s value is more than or equal to its right character’s value, the condition is true.

Example 1: Case sensitive Comparison

Lisp




(write (CHAR= #\a #\A))
(terpri)
(write (CHAR> #\b #\a))
(terpri)
(write (CHAR< #\A #\a))

Output: When running the above code, the output is as follows.

NIL
T
T

Example 2: Case-insensitive Comparison

Lisp




(write (CHAR-EQUAL #\a #\A))
(terpri)
(write (CHAR-EQUAL #\a #\b))
(terpri)
(write (CHAR-LESSP #\a #\b #\c))
(terpri)
(write (CHAR-GREATERP #\a #\b #\c))

Output: When running the above code, the output is as follows.

T
NIL
T
NIL

Comparison Operators on String:

In Common Lisp, strings are vectors, which are one-dimensional arrays of characters. Except for the double quotation character (“) and the escape character (\), every character supported by the character set can be contained between double quotes to form a string. Like Character, the string also has two sets of Comparison Operators, one is Case-sensitive and the other is Case-insensitive.

So, to check whether the two Strings are equal are not, the following are some comparison Functions that can be used.

Case-sensitiveCase-insensitiveExplanation
STRING=STRING-EQUALChecks if the operands’ values are all equal; if they are, the condition is true.
STRING/=STRING-NOT-EQUALChecks if the operands’ values are all unequal; if they are, the condition is true.
STRING<STRING-LESSPIf string1 is smaller than string2, the condition is true; otherwise, the condition is false.
STRING<=     STRING-NOT-GREATERP    If any of the left operands’ values are less than or equal to the value of the following right operand, the condition is true.
STRING>STRING-GREATERPIf string1 is greater than string2, the condition is true; otherwise, the condition is false.
STRING>=               STRING-NOT-LESSP                          If any left operand’s value is more than or equal to its right operand’s value, the condition is true.  

Example 1: Case Sensitive Comparison.

Lisp




(write (STRING= "gfg" "GFG"))
(terpri)
(write (STRING> "gfg" "GFG"))
(terpri)
(write (STRING< "gfg" "GFG"))

Output: When running the above code, the output is as follows.

NIL
0
NIL

Example 2: Case-insensitive Comparison.

Lisp




(write (STRING-EQUAL "gfg" "GFG"))
(terpri)
(write (STRING-GREATERP "gfg" "GFG"))
(terpri)
(write (STRING-LESSP "gfg" "GFG"))

Output: Comparison OperatorsWhen running the above code, the output is as follows.

T
NIL
NIL

My Personal Notes arrow_drop_up
Last Updated : 25 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials