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
Similar Reads
Atoms in LISP Pre-requisites: Introduction to LISP Function Languages are those languages in which the basic building block is functions. In functional programming, the programmer is concerned only with functionality and not memory related variable storage and assignment sequence. There are some commonly used Fun
2 min read
Symbols in LISP Symbols are lisp data objects and every type of symbol object has a name called its print name. Symbol names may contain any combination of letters and numbers, plus some special characters such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter characters l
4 min read
Sets in LISP A set is an unordered collection of items. A set is just like other data structures. In C++ we have a similar data structure called a hash map. Common lisp does not provide a built-in set data type, but it provides a number of functions that allow set operations to be performed onto a list. Using th
7 min read
Perl | Hash in Scalar and List Context Prerequisite: Perl Hash Hash in Perl is a set of key/value pairs. Perl provides us the flexibility to assign the hash to a List type and a Scalar type, known as LIST Context and SCALAR Context respectively. Hash in LIST Context The assignment of a hash to a list type in Perl is accomplished by makin
5 min read
Predicates in LISP In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Structures in LISP LISP, is a list processing, is a programming language widely used in working with data manipulation. Structures are used defines data types, that have the ability to combine with another data type to complete the given task. Attribute used: The defstruct attribute is used to create an instance of a
2 min read