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 |
---|---|
1 | atomIt takes one argument and returns t if the argument is an atom or nil if otherwise. |
2 | equalIt takes two arguments and returns t if they are structurally equal or nil otherwise. |
3 | eqIt takes two arguments and returns t if they are same identical objects, sharing the same memory location or nil otherwise. |
4 | eqlIt 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. |
5 | evenpIt takes one numeric argument and returns t if the argument is even number or nil if otherwise. |
6 | oddpIt takes one numeric argument and returns t if the argument is odd number or nil if otherwise. |
7 | zeropIt takes one numeric argument and returns t if the argument is zero or nil if otherwise. |
8 | nullIt takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil. |
9 | listpIt takes one argument and returns t if the argument evaluates to a list otherwise it returns nil. |
10 | greaterpIt 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. |
11 | lesspIt 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. |
12 | numberpIt takes one argument and returns t if the argument is a number or nil if otherwise. |
13 | symbolpIt takes one argument and returns t if the argument is a symbol otherwise it returns nil. |
14 | integerpIt takes one argument and returns t if the argument is an integer otherwise it returns nil. |
15 | rationalpIt takes one argument and returns t if the argument is rational number, either a ratio or a number, otherwise it returns nil. |
16 | floatpIt takes one argument and returns t if the argument is a floating point number otherwise it returns nil. |
17 | realpIt takes one argument and returns t if the argument is a real number otherwise it returns nil. |
18 | complexpIt takes one argument and returns t if the argument is a complex number otherwise it returns nil. |
19 | characterpIt takes one argument and returns t if the argument is a character otherwise it returns nil. |
20 | stringpIt takes one argument and returns t if the argument is a string object otherwise it returns nil. |
21 | arraypIt takes one argument and returns t if the argument is an array object otherwise it returns nil. |
22 | packagepIt 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
Leave a Reply