Hash Table in LISP
A hash table is a type of collection in Common LISP, that is used to map keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
Types of Hash Tables in LISP:
There are three types of hashtables in lisp, which are listed below:
- eq : It is used for comparison when the hashing of the hash table is done on LISP objects.
- eql: It is also used for comparison when the hashing of the hash table is done on LISP objects.
- equal: It is used for comparison when the hashing of the hash table is done on LISP tree structures.
Creating Hash Table in LISP:
The make-hash-table function is used in Common LISP to create a hash table.
Syntax:
make-hash-table &key :test :size :rehash-size : rehash-threshold
Here,
- key:- It is the key name.
- :test:- It is used to determine how the keys are compared. It takes one of the three types of hash table values mentioned above(ie, eq, eql, equal).
- :size:- It is used to set the initial size of the hash table.
- :rehash-size:- It is used to set the increment in the size of the hash table once it gets full and more data needs to be added.
- :rehash-threshold:- It is used to set the maximum size of the hashtable after which the increment can be done to its size.
Adding & Fetching Data from Hash Table in LISP:
The gethash function is used to fetch data from a hash table in LISP.
Syntax:
gethash key hash-table &optional default
Here,
- key: It is the key name.
- hash-table: It is the name of the hashtable
- default: It is the return type. If not set it returns nil if the value is not found.
The setf function is used with the gethash function to add data to a hash table in LISP.
Syntax:
setf (gethash 'key hash-table-name) '(value)
Here,
- key: It is the key name.
- hash-table-name: It is the name of the hashtable
- value: It is the value to be associated with the key
Example: Creating a hash table, adding data, and fetching data from it.
Lisp
; call make - hash - table function (setq makeHashTable (make - hash - table)) ; 1st entry ( setf (gethash 'gfg makeHashTable) ' (Geeksforgeeks)) ; 2nd entry ( setf (gethash 'DSA makeHashTable) ' (Data Structure & Algorithms)) ; output 1 (write (gethash 'gfg makeHashTable)) (terpri) ; output 2 (write (gethash 'DSA makeHashTable)) |
Output:
(GEEKSFORGEEKS) (DATA STRUCTURE & ALGORITHMS)
Removing Entries from Hash Table in LISP:
The remhash function is used to remove key-value entries from a hash table.
Syntax:
remhash key hash-table
Here,
- key: It is the name of the key.
- hash-table: It is the name of the hash table from where the entries are to be removed.
Example: Here we will remove the 1st entry done in the previous example and print out the result.
Lisp
; call make - hash - table function (setq makeHashTable (make - hash - table)) ; 1st entry ( setf (gethash 'gfg makeHashTable) ' (Geeksforgeeks)) ; 2nd entry ( setf (gethash 'DSA makeHashTable) ' (Data Structure & Algorithms)) ; output 1 (write (gethash 'gfg makeHashTable)) (terpri) ; output 2 (write (gethash 'DSA makeHashTable)) ; remove 1st entry (remhash 'gfg makeHashTable) (terpri) ; output the first entry (write (gethash 'gfg makeHashTable)) |
Output:
(GEEKSFORGEEKS) (DATA STRUCTURE & ALGORITHMS) NIL
Please Login to comment...