Selected Reading

Lisp - Specific Data Type Predicates



Specific Data Type Predicates are special functions that test their arguments for particular 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

integerp

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

2

rationalp

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

3

floatp

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

4

realp

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

5

complexp

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

6

characterp

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

7

stringp

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

Example - integerp

Following code check if object is an integer.

main.lisp

(write(integerp 10))     ; T
(terpri)
(write(integerp 3.14))   ; NIL

Output

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

T
NIL

Example - rationalp

Following code check if object is either a ratio or a number.

main.lisp

(write(rationalp 10))    ; T
(terpri)
(write(rationalp 3/4))   ; T
(terpri)
(write(rationalp 3.14))  ; NIL

Output

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

T
NIL
NIL

Example - floatp

Following code check if object is a float.

main.lisp

(write(floatp 10))     ; NIL
(terpri)
(write(floatp 3.14))   ; T

Output

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

T
NIL

Example - realp

Following code check if object is a real number, that is either rational or a float number.

main.lisp

(write(realp 10))    ; T
(terpri)
(write(realp 3/4))   ; T
(terpri)
(write(realp 3.14))  ; T 
(terpri)
(write(realp #c(1 2)))  ; NIL, complex number is not real

Output

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

T
T
T 
NIL

Example - complexp

Following code check if object is a complex number.

main.lisp

(write(complexp 3.14))  ; Nil 
(terpri)
(write(complexp #c(1 2)))  ; T

Output

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

T
T
T 
NIL

Example - characterp

Following code check if object is a character.

main.lisp

(write(characterp #\A)); T
(terpri)
(write(characterp 3.14))   ; NIL
(terpri)
(write(characterp 'x))     ; NIL

Output

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

T
NIL
NIL

Example - stringp

Following code check if object is a string.

main.lisp

(write(stringp "hello")); T
(terpri)
(write(stringp 3.14))   ; NIL
(terpri)
(write(stringp 'x))     ; NIL

Output

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

T
NIL
NIL
Advertisements