Lisp - Logical Predicates



Lisp provides various logical 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 to check logical conditions.

The following table shows some of the most commonly used logical predicates −

Sr.No. Predicate & Description
1

and

Returns t if all arguments are true or nil if otherwise.

2

or

Returns t if any of the arguments is true or nil if otherwise.

3

not

Returns t if arguments is false or nil if otherwise.

Example and Predicate

Following code check and predicates on different values.

main.lisp

(write(and t t))        ; T
(terpri)
(write(and t nil))      ; NIL
(terpri)
(write(and nil nil))    ; NIL

Output

When you execute the code, it returns the following result −

T
NIL
NIL

Example or Predicate

Following code check and predicates on different values.

main.lisp

(write(or t t))        ; T
(terpri)
(write(or t nil))      ; T
(terpri)
(write(or nil nil))    ; NIL

Output

When you execute the code, it returns the following result −

T
T
NIL

Example not Predicate

Following code check and predicates on different values.

main.lisp

(write(not t))          ; NIL
(terpri)
(write(not nil))        ; T

Output

When you execute the code, it returns the following result −

NIL
T
Advertisements