Selected Reading

Lisp - Generic Data Type Predicates



Generic Data Type Predicates are special functions that test their arguments for data types and returns True as t if object is of that type otherwise false as nil if the object is not of that type.

The following table shows some of the most commonly used data type 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

listp

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

3

numberp

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

4

symbolp

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

5

arrayp

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

Example - atom

Following code check if object is an atom, that is number, symbol, character or string.

main.lisp

(write(atom 10))        ; T
(terpri)
(write(atom 'x))        ; T
(terpri)
(write(atom "hello"))   ; T
(terpri)
(write(atom '(a b)))    ; NIL

Output

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

T
T
T
NIL

Example - listp

Following code check if object is a list, that is cons cell.

main.lisp

(write(listp '(a b)))   ; T
(terpri)
(write(listp nil))      ; T, nil is an empty list
(terpri)
(write(listp 10))       ; NIL

Output

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

T
T
NIL

Example - numberp

Following code check if object is a number like integer, float etc.

main.lisp

(write(numberp 10))     ; T
(terpri)
(write(numberp 3.14))   ; T
(terpri)
(write(numberp 'x))     ; NIL

Output

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

T
T
NIL

Example - symbolp

Following code check if object is a symbol.

main.lisp

(write(symbolp 'x))     ; T
(terpri)
(write(symbolp '+))     ; T
(terpri)
(write(symbolp 10))     ; NIL

Output

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

T
T
NIL

Example - arrayp

Following code check if object is an array.

main.lisp

(setf my-array (make-array '(10))) ; create an array

(write(arrayp my-array)); T
(terpri)
(write(arrayp nil)); NIL 

Output

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

T
NIL
Advertisements