Predicates

Predicates are functions that test their arguments for some specific conditions and returns nil if the condition is false, or some non-nil value is the condition is true.

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

Sr.No.Predicate & Description
1atomIt takes one argument and returns t if the argument is an atom or nil if otherwise.
2equalIt takes two arguments and returns t if they are structurally equal or nil otherwise.
3eqIt takes two arguments and returns t if they are same identical objects, sharing the same memory location or nil otherwise.
4eqlIt takes two arguments and returns t if the arguments are eq, or if they are numbers of the same type with the same value, or if they are character objects that represent the same character, or nil otherwise.
5evenpIt takes one numeric argument and returns t if the argument is even number or nil if otherwise.
6oddpIt takes one numeric argument and returns t if the argument is odd number or nil if otherwise.
7zeropIt takes one numeric argument and returns t if the argument is zero or nil if otherwise.
8nullIt takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil.
9listpIt takes one argument and returns t if the argument evaluates to a list otherwise it returns nil.
10greaterpIt takes one or more argument and returns t if either there is a single argument or the arguments are successively larger from left to right, or nil if otherwise.
11lesspIt takes one or more argument and returns t if either there is a single argument or the arguments are successively smaller from left to right, or nil if otherwise.
12numberpIt takes one argument and returns t if the argument is a number or nil if otherwise.
13symbolpIt takes one argument and returns t if the argument is a symbol otherwise it returns nil.
14integerpIt takes one argument and returns t if the argument is an integer otherwise it returns nil.
15rationalpIt takes one argument and returns t if the argument is rational number, either a ratio or a number, otherwise it returns nil.
16floatpIt takes one argument and returns t if the argument is a floating point number otherwise it returns nil.
17realpIt takes one argument and returns t if the argument is a real number otherwise it returns nil.
18complexpIt takes one argument and returns t if the argument is a complex number otherwise it returns nil.
19characterpIt takes one argument and returns t if the argument is a character otherwise it returns nil.
20stringpIt takes one argument and returns t if the argument is a string object otherwise it returns nil.
21arraypIt takes one argument and returns t if the argument is an array object otherwise it returns nil.
22packagepIt takes one argument and returns t if the argument is a package otherwise it returns nil.

Example 1

Create a new source code file named main.lisp and type the following code in it.

(write (atom 'abcd))
(terpri)
(write (equal 'a 'b))
(terpri)
(write (evenp 10))
(terpri)
(write (evenp 7 ))
(terpri)
(write (oddp 7 ))
(terpri)
(write (zerop 0.0000000001))
(terpri)
(write (eq 3 3.0 ))
(terpri)
(write (equal 3 3.0 ))
(terpri)
(write (null nil ))

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

T
NIL
T
NIL
T
NIL
NIL
NIL
T

Example 2

Create a new source code file named main.lisp and type the following code in it.

(defun factorial (num)
   (cond ((zerop num) 1)
      (t ( * num (factorial (- num 1))))
   )
)
(setq n 6)
(format t "~% Factorial ~d is: ~d" n (factorial n))

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

Factorial 6 is: 720

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *