remhash
remhash Function
Syntax:
remhash key hash-table → generalized-boolean
Arguments and Values:
key—an object.
hash-table—a hash table.
generalized-boolean—a generalized boolean.
Description:
Removes the entry for key in hash-table, if any. Returns true if there was such an entry, or false otherwise.
Examples:
(setq table (make-hash-table)) → #<HASH-TABLE EQL 0/120 32115666>
(setf (gethash 100 table) "C") → "C"
(gethash 100 table) → "C", *true*
(remhash 100 table) → true
(gethash 100 table) → NIL, *false*
(remhash 100 table) → false
Side Effects:
The hash-table is modified.
Hash
maphashExpanded Reference: remhash
Basic Removal
remhash removes an entry by key and returns true if the entry existed, false otherwise.
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1)
(remhash 'a ht))
=> T
(let ((ht (make-hash-table)))
;; Removing a key that does not exist
(remhash 'nonexistent ht))
=> NIL
Verifying Removal
(let ((ht (make-hash-table)))
(setf (gethash 'x ht) 42)
(gethash 'x ht))
=> 42
=> T
(let ((ht (make-hash-table)))
(setf (gethash 'x ht) 42)
(remhash 'x ht)
(gethash 'x ht))
=> NIL
=> NIL
Effect on Count
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1)
(setf (gethash 'b ht) 2)
(setf (gethash 'c ht) 3)
(format nil "Before: ~D" (hash-table-count ht)))
=> "Before: 3"
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1)
(setf (gethash 'b ht) 2)
(setf (gethash 'c ht) 3)
(remhash 'b ht)
(format nil "After: ~D" (hash-table-count ht)))
=> "After: 2"
Idempotent Removal
Calling remhash twice on the same key is safe; the second call simply returns nil.
(let ((ht (make-hash-table)))
(setf (gethash 'key ht) "value")
(list (remhash 'key ht)
(remhash 'key ht)))
=> (T NIL)
Removing During Iteration with maphash
It is permitted to use remhash on the current entry during maphash.
;; Remove all entries with even values
(let ((ht (make-hash-table)))
(dotimes (i 6)
(setf (gethash i ht) i))
(maphash (lambda (key value)
(when (evenp value)
(remhash key ht)))
ht)
(hash-table-count ht))
=> 3