characterp
characterp Function
Syntax:
characterp object → generalized-boolean
Arguments and Values:
object—an object.
generalized-boolean—a generalized boolean.
Description:
Returns true if object is of type character; otherwise, returns false.
Examples:
(characterp #\a) → true
(characterp ’a) → false
(characterp "a") → false
(characterp 65.) → false
(characterp #\Newline) → true
;; This next example presupposes an implementation
;; in which #\Rubout is an implementation-defined character.
(characterp #\Rubout) → true
See Also:
character (type and function), typep
Notes:
(characterp object) ≡ (typep object ’character)
Expanded Reference: characterp
Basic type checking
characterp returns true if the object is a character, false otherwise.
(characterp #\a)
=> T
(characterp #\Space)
=> T
(characterp #\Newline)
=> T
Non-character objects
Strings, symbols, and numbers are not characters, even if they represent or contain characters.
(characterp "a")
=> NIL
(characterp 'a)
=> NIL
(characterp 65)
=> NIL
(characterp nil)
=> NIL
(characterp "")
=> NIL
Equivalence with typep
characterp is equivalent to (typep object 'character).
(eql (characterp #\x) (typep #\x 'character))
=> T
(eql (characterp 42) (typep 42 'character))
=> T
Using characterp as a predicate
characterp can be used with higher-order functions to filter or test collections.
(every #'characterp '(#\a #\b #\c))
=> T
(every #'characterp '(#\a "b" #\c))
=> NIL
(remove-if-not #'characterp '(#\a 1 #\b "c"))
=> (#\a #\b)
Characters extracted from strings
Individual characters extracted from strings via char or aref are always character objects.
(characterp (char "hello" 0))
=> T
(characterp (aref "world" 2))
=> T