hash-table-p
hash-table-p Function
Syntax:
hash-table-p object → generalized-boolean
Arguments and Values:
object—an object.
generalized-boolean—a generalized boolean.
Description:
Returns true if object is of type hash-table; otherwise, returns false.
Hash
Examples:
(setq table (make-hash-table)) → #<HASH-TABLE EQL 0/120 32511220>
(hash-table-p table) → true
(hash-table-p 37) → false
(hash-table-p ’((a . 1) (b . 2))) → false
Notes:
(hash-table-p object) ≡ (typep object ’hash-table)
Expanded Reference: hash-table-p
Basic Type Predicate
hash-table-p returns true if its argument is a hash table, false otherwise.
(hash-table-p (make-hash-table))
=> T
(hash-table-p 42)
=> NIL
(hash-table-p "hello")
=> NIL
Not Confused by Similar Structures
Association lists and property lists are not hash tables.
(hash-table-p '((a . 1) (b . 2) (c . 3)))
=> NIL
(hash-table-p '(a 1 b 2 c 3))
=> NIL
(hash-table-p nil)
=> NIL
Works with All Hash Table Variants
(hash-table-p (make-hash-table :test #'eq))
=> T
(hash-table-p (make-hash-table :test #'equal))
=> T
(hash-table-p (make-hash-table :test #'equalp))
=> T
Equivalent to typep
;; hash-table-p is equivalent to (typep object 'hash-table)
(let ((ht (make-hash-table)))
(eq (hash-table-p ht)
(typep ht 'hash-table)))
=> T
Useful in Defensive Programming
(defun safe-lookup (key table)
"Look up KEY in TABLE, signaling an error if TABLE is not a hash table."
(unless (hash-table-p table)
(error "Expected a hash table, got ~S" table))
(gethash key table))
(safe-lookup 'x (make-hash-table))
=> NIL
=> NIL