LISP - Comparison Operators on Characters & Strings
Last Updated :
25 Feb, 2022
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-Sensitive | Case-Insensitive | Explanation |
---|
CHAR= | CHAR-EQUAL | Checks if the operands' values are all equal; if they are, the condition is true. |
CHAR/= | CHAR-NOT-EQUAL | Checks if the operands' values are all different or not; if they aren't, the condition is true. |
CHAR< | CHAR-LESSP | If the character1 is smaller than character2, the condition is true; otherwise, the condition is false. |
CHAR<= | CHAR-NOT-GREATERP | If 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-GREATERP | If 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-sensitive | Case-insensitive | Explanation |
---|
STRING= | STRING-EQUAL | Checks if the operands' values are all equal; if they are, the condition is true. |
STRING/= | STRING-NOT-EQUAL | Checks if the operands' values are all unequal; if they are, the condition is true. |
STRING< | STRING-LESSP | If 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-GREATERP | If 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
Similar Reads
What are Comparison Operators in R?
Comparison operators are fundamental tools in any programming language, allowing you to compare values and make decisions based on the results. In R Programming Language comparison operators are used to compare numeric, string, and other types of data. They return a logical value (TRUE or FALSE) bas
3 min read
Comparison Operators in Programming
Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional
10 min read
Comparison Operators in LISP
In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the
4 min read
Comparison Operators in Solidity
Comparison Operators are used to compare two values. Solidity has the following types of comparison operators: Greater than: Greater than operator compares two operands. It evaluates to true when the left operand is greater than the right operand otherwise false. It is denoted by >.Lesser than: L
3 min read
C++ Comparison Operators
Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result. There are mainly 6 Comparison Operators namely: Greater than (>) : this operator checks whether operand1 is greater than operand2. If the result t
3 min read
Excel VBA Comparison Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Some helpful links to get more insights about Macros, VBA in Excel : 1. Record Macros in E
5 min read
JavaScript Comparison Operators
JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. 1. Equality Operator (==) The Equality operator is used to compare the equality of two operands. JavaScript// Illustration of (==) operator let x = 5; let y = '5'; // Checking of operands c
5 min read
Basic String Operations with Implementation
In this post, we will look into some of the basic String operations such as:Table of ContentAccessing characters by index in a string.Inserting Character/String into an String.Modifying character in StringDeletion of character in StringConcatenating strings (combining multiple strings into one).Find
14 min read
Characters in LISP
In Lisp data objects of type 'character' are referred to as characters. For representation purposes, we usually denote character objects by preceding a #\ symbol before the character. Any character can be represented by using the #\ symbol before the name of the character. For Example #\a represents
2 min read
Char Comparison in C
Char is a keyword used for representing characters in C. Character size in C is 1 byte. There are two methods to compare characters in C and these are: Using ASCII valuesUsing strcmp( ) .1. Using ASCII values to compare characters The first method is pretty simple, we all know that each character ca
3 min read