English 中文(简体)
LISP - Predicates
  • 时间:2024-12-22

LISP - Predicates


Previous Page Next Page  

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

atom

It takes one argument and returns t if the argument is an atom or nil if otherwise.

2

equal

It takes two arguments and returns t if they are structurally equal or nil otherwise.

3

eq

It takes two arguments and returns t if they are same identical objects, sharing the same memory location or nil otherwise.

4

eql

It 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

evenp

It takes one numeric argument and returns t if the argument is even number or nil if otherwise.

6

oddp

It takes one numeric argument and returns t if the argument is odd number or nil if otherwise.

7

zerop

It takes one numeric argument and returns t if the argument is zero or nil if otherwise.

8

null

It takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil.

9

pstp

It takes one argument and returns t if the argument evaluates to a pst otherwise it returns nil.

10

greaterp

It 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

lessp

It 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

numberp

It takes one argument and returns t if the argument is a number or nil if otherwise.

13

symbolp

It takes one argument and returns t if the argument is a symbol otherwise it returns nil.

14

integerp

It takes one argument and returns t if the argument is an integer otherwise it returns nil.

15

rationalp

It takes one argument and returns t if the argument is rational number, either a ratio or a number, otherwise it returns nil.

16

floatp

It takes one argument and returns t if the argument is a floating point number otherwise it returns nil.

17

realp

It takes one argument and returns t if the argument is a real number otherwise it returns nil.

18

complexp

It takes one argument and returns t if the argument is a complex number otherwise it returns nil.

19

characterp

It takes one argument and returns t if the argument is a character otherwise it returns nil.

20

stringp

It takes one argument and returns t if the argument is a string object otherwise it returns nil.

21

arrayp

It takes one argument and returns t if the argument is an array object otherwise it returns nil.

22

packagep

It 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.psp 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.psp 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
Advertisements