Skip to main content

keywordp

keywordp Function

Syntax:

keywordp object → generalized-boolean

Arguments and Values:

object—an object.

generalized-boolean—a generalized boolean.

Description:

Returns true if object is a keyword 1; otherwise, returns false.

Examples:

(keywordp ’elephant) → false 
(keywordp 12) → false
(keywordp :test) → true
(keywordp:test) → true
(keywordp nil) → false
(keywordp :nil) → true
(keywordp(:test)) → false
(keywordp "hello") → false
(keywordp ":hello") → false
(keywordp ’&optional) → false

See Also:

constantp, keyword, symbolp, symbol-package

Expanded Reference: keywordp

Basic usage

keywordp returns true if the object is a keyword (a symbol interned in the KEYWORD package), and false otherwise.

(keywordp :test)
=> T
(keywordp :hello)
=> T
(keywordp :nil)
=> T

Non-keyword symbols

Regular symbols, even if their names look like keywords, are not keywords.

(keywordp 'test)
=> NIL
(keywordp 'hello)
=> NIL

NIL is not a keyword

Despite being a constant, nil is interned in the COMMON-LISP package, not KEYWORD.

(keywordp nil)
=> NIL
(keywordp t)
=> NIL

Non-symbol types

keywordp returns false for all non-symbol objects.

(keywordp 42)
=> NIL
(keywordp "hello")
=> NIL
(keywordp ":hello")
=> NIL
(keywordp '(:a :b))
=> NIL

Keywords are self-evaluating

Keywords evaluate to themselves, which is one reason keywordp is useful for validation.

(let ((k :mode))
(values (keywordp k)
(eq k (symbol-value k))))
=> T
=> T

Filtering keywords from a list

(remove-if-not #'keywordp '(:a b :c 42 "d" :e nil))
=> (:A :C :E)