Lisp - Comparison Predicates
Lisp provides various useful comparison predicates which tests for a condition and return t as true and nil as false. These predicates are mainly useful for decision making and control flow statements like cond, if, when and unless statements. These predicates can be treated as operators as well.
The following table shows some of the most commonly used comparison predicates −
| Sr.No. | Predicate & Description |
|---|---|
| 1 | < Less than. It takes two arguments and returns t if first argument is lesser than second argument or nil if otherwise. |
| 2 | > Greater than. It takes two arguments and returns t if first argument is greater than second argument or nil if otherwise. |
| 3 | <= Less than or equal to. It takes two arguments and returns t if first argument is less than or equal to second argument or nil if otherwise. |
| 4 | >= Greater than or equal to. It takes two arguments and returns t if first argument is greater than or equal to second argument or nil if otherwise. |
| 5 | = equal to. It takes two numbers as arguments and returns t if first argument is equal to second argument or nil if otherwise. |
Example < Predicate
Following code check if first number is less than second number.
main.lisp
(write(< 5 3)) ; NIL (terpri) (write(< 3 5)) ; T
Output
When you execute the code, it returns the following result −
NIL T
Example > Predicate
Following code check if first number is greater than second number.
main.lisp
(write(> 5 3)) ; T (terpri) (write(> 3 5)) ; NIL
Output
When you execute the code, it returns the following result −
T NIL
Example <= Predicate
Following code check if first number is less than second number.
main.lisp
(write(<= 5 3)) ; NIL (terpri) (write(<= 3 5)) ; T (terpri) (write(<= 5 5)) ; T
Output
When you execute the code, it returns the following result −
NIL T T
Example >= Predicate
Following code check if first number is greater than second number.
main.lisp
(write(>= 5 3)) ; T (terpri) (write(>= 3 5)) ; NIL (terpri) (write(>= 5 5)) ; T
Output
When you execute the code, it returns the following result −
T NIL T
Example = Predicate
Following code check if first number is equal to second number.
main.lisp
(write(= 5 3)) ; NIL (terpri) (write(= 5 5)) ; T
Output
When you execute the code, it returns the following result −
NIL T