
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp - Hash Table
The hash table data structure represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.
A hash table is used when you need to access elements by using a key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
Creating Hash Table in LISP
In Common LISP, hash table is a general-purpose collection. You can use arbitrary objects as a key or indexes.
When you store a value in a hash table, you make a key-value pair, and store it under that key. Later you can retrieve the value from the hash table using the same key. Each key maps to a single value, although you can store a new value in a key.
Hash tables, in LISP, could be categorized into three types, based on the way the keys could be compared - eq, eql or equal. If the hash table is hashed on LISP objects then the keys are compared with eq or eql. If the hash table hash on tree structure, then it would be compared using equal.
The make-hash-table function is used for creating a hash table. Syntax for this function is −
make-hash-table −key :test :size :rehash-size :rehash-threshold
Where −
The key argument provides the key.
The :test argument determines how keys are compared - it should have one of three values #'eq, #'eql, or #'equal, or one of the three symbols eq, eql, or equal. If not specified, eql is assumed.
The :size argument sets the initial size of the hash table. This should be an integer greater than zero.
The :rehash-size argument specifies how much to increase the size of the hash table when it becomes full. This can be an integer greater than zero, which is the number of entries to add, or it can be a floating-point number greater than 1, which is the ratio of the new size to the old size. The default value for this argument is implementation-dependent.
The :rehash-threshold argument specifies how full the hash table can get before it must grow. This can be an integer greater than zero and less than the :rehash-size (in which case it will be scaled whenever the table is grown), or it can be a floating-point number between zero and 1. The default value for this argument is implementation-dependent.
You can also call the make-hash-table function with no arguments.
Retrieving Items from and Adding Items into the Hash Table
The gethash function retrieves an item from the hash table by searching for its key. If it does not find the key, then it returns nil.
It has the following syntax −
gethash key hash-table −optional default
where −
key: is the associated key
hash-table: is the hash-table to be searched
default: is the value to be returned, if the entry is not found, which is nil, if not specified.
The gethash function actually returns two values, the second being a predicate value that is true if an entry was found, and false if no entry was found.
For adding an item to the hash table, you can use the setf function along with the gethash function.
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; create a hashtable (setq empList (make-hash-table)) (setf (gethash '001 empList) '(Charlie Brown)) (setf (gethash '002 empList) '(Freddie Seal)) ; print the value for 001 from hashtable (write (gethash '001 empList)) ; terminate printing (terpri) ; print the value for 002 from hashtable (write (gethash '002 empList))
Output
When you execute the code, it returns the following result −
(CHARLIE BROWN) (FREDDIE SEAL)
Removing an Entry
The remhash function removes any entry for a specific key in hash-table. This is a predicate that is true if there was an entry or false if there was not.
The syntax for this function is −
remhash key hash-table
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; create a hashtable (setq empList (make-hash-table)) (setf (gethash '001 empList) '(Charlie Brown)) (setf (gethash '002 empList) '(Freddie Seal)) (setf (gethash '003 empList) '(Mark Mongoose)) ; print the hash for 001 from hashtable (write (gethash '001 empList)) ; terminate printing (terpri) ; print the hash for 002 from hashtable (write (gethash '002 empList)) ; terminate printing (terpri) ; print the hash for 003 from hashtable (write (gethash '003 empList)) ; remove hash value (remhash '003 empList) ; terminate printing (terpri) ; print the hash for 001 from hashtable (write (gethash '003 empList))
Output
When you execute the code, it returns the following result −
(CHARLIE BROWN) (FREDDIE SEAL) (MARK MONGOOSE) NIL
The maphash Function
The maphash function allows you to apply a specified function on each key-value pair on a hash table.
It takes two arguments - the function and a hash table and invokes the function once for each key/value pair in the hash table.
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; create a hashtable (setq empList (make-hash-table)) (setf (gethash '001 empList) '(Charlie Brown)) (setf (gethash '002 empList) '(Freddie Seal)) (setf (gethash '003 empList) '(Mark Mongoose)) ; map hash on hashtable (maphash #'(lambda (k v) (format t "~a => ~a~%" k v)) empList)
Output
When you execute the code, it returns the following result −
3 => (MARK MONGOOSE) 2 => (FREDDIE SEAL) 1 => (CHARLIE BROWN)