Open In App

Predicates in LISP

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T.

Types of predicates:

Below is a list of major Predicates with syntax and examples.

1. atom:

This predicate will return t if the argument is an atom otherwise it will return nil.

Syntax:

(atom 'element)

Example:

Lisp




;check atom or not
(write (atom 'geeks))
(terpri)


Output:

T

2. equal:

This predicate will check the given two arguments are equal or not, if equal it returns T otherwise nil.

Syntax:

(equal 'element1 'element2)

Example:

Lisp




;check equal or not
(write (equal 'geeks 'forgeeks))
(terpri)
  
;check equal or not
(write (equal 'python 'python))
(terpri)


Output:

NIL
T

3. eq:

This predicate will check will two arguments are identical in the case of sharing the same memory

It will return t if they are identical, otherwise nil

Syntax:

(eq 'element1 'element2)

Example:

Lisp




;check equal or not
(write (eq 'geeks 'forgeeks))
(terpri)
  
;check equal or not
(write (eq 45 45))
(terpri)


Output:

NIL
T

4. evenp:

This predicate will check the given number is even or not. It will return t if the number is even otherwise nil

Syntax:

(evenp element)

Example:

Lisp




;check even or not
(write (evenp 31))
(terpri)
  
;check even or not
(write (evenp 20))
(terpri)


Output:

NIL
T

5. oddp:

This predicate will check the given number is odd or not.

It will return t if the number is odd otherwise nil

Syntax:

(oddp element)

Example:

Lisp




;check odd or not
(write (oddp 31))
(terpri)
  
;check odd or not
(write (oddp 20))
(terpri)


Output:

T
NIL

6. zerop:

This predicate will check the given number is zero or not

it will return t if the number is zero otherwise nil

Syntax:

(zerop element)

Example:

Lisp




;check zero or not
(write (zerop 0))
(terpri)
  
;check zero or not
(write (zerop 20))
(terpri)


Output:

T
NIL

7. null:

This predicate will check the given element is nil or not, it returns T when the given element in nil otherwise NIL

Syntax:

(null element)

Example:

Lisp




;check null or not
(write (null 0))
(terpri)
  
;check null or not
(write (null nil))
(terpri)


Output:

NIL
T

8. listp:

It will return t if the given element is in a list otherwise nil

Syntax:

(listp  element)

Example:

Lisp




;check list or not
(write (listp 0))
(terpri)
  
;check list or not
(write (listp  '(g e e k s)))
(terpri)


Output:

NIL
T   

9. numberp:

This predicate will return if the given argument is number otherwise nil

Syntax:

(numberp element)

Example:

Lisp




;check number or not
(write (numberp 67  ))
(terpri)
  
;check number or not
(write (numberp  '(g e e k s)))
(terpri)


Output:

T
NIL

10. integerp:

This predicate will return t if it is an integer otherwise nil will return

Syntax:

(integerp element)

Example:

Lisp




;check integer or not
(write (integerp 67  ))
(terpri)
  
;check integer or not
(write (integerp  '(g e e k s)))
(terpri)


Output:

T
NIL

11. rationalp:

This predicate will return t if the argument  is a rational number otherwise nil will return

Syntax:

(rationalp element)

Example:

Lisp




;check rational or not
(write (rationalp 67 ))
(terpri)
  
;check rational or not
(write (rationalp  '(g e e k s)))
(terpri)


Output:

T
NIL

12. complex:

This predicate will return t if the argument  is a complex number otherwise nil will return

Syntax:

(complexp element)

Example:

Lisp




;check complex or not
(write (complexp #c( 10 30) ))
(terpri)
  
;check complex or not
(write (rationalp  '(g e e k s)))
(terpri)


Output:

T
NIL

13. characterp:

This predicate will return t if the argument  is a character otherwise nil will return

Syntax:

(characterp element)

Example:

Lisp




;check character or not
(write (characterp 'h ))
(terpri)
  
;check character or not
(write (characterp  '(g e e k s)))
(terpri)


Output:

NIL
NIL

14. floatp:

This predicate will return t if the argument  is an floating number otherwise nil will return

Syntax:

(floatp element)

Example:

Lisp




;check float or not
(write (floatp  56.78 ))
(terpri)
  
;check float or not
(write (floatp  67))
(terpri)


Output:

T
NIL

15. realp:

This predicate will check the given number is real or not. It will return t if it is real otherwise nil

Syntax:

(realp  element)

Example:

Lisp




;check real or not
(write (realp  56.78 ))
(terpri)
  
;check real or not
(write (realp  67))
(terpri)


Output:

T
T

16. stringp:

This predicate will check the given argument is a string or not. It will return t if it is string otherwise nil

Syntax:

(stringp  element)

Example:

Lisp




;check string or not
(write (stringp  "heelo geeks" ))
(terpri)
  
;check string or not
(write (stringp  67))
(terpri)


Output:

T
NIL

17. arrayp:

This predicate will return t if the argument is an array otherwise nil.

Syntax:

(arrayp element)

Example:

Lisp




;create an array
(write (setf array1 (make-array '(10))))
(terpri)
  
  
;check array or not
(write (arrayp  array1))
(terpri)
  
;check array or not
(write (arrayp  90))
(terpri)


Output:

#(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
T
NIL


Last Updated : 26 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads