Hash tables in Common Lisp are data structures for storing associative data. They map keys (which can be any object) to values (which can be any object). Other languages may refer to this concept as maps, dictionaries or hashes.
Hash tables can be created with the make-hash-table
function.
(make-hash-table) ; => #<HASH-TABLE :TEST EQL :COUNT 0 {10024AD2B3}>
(Note the printed presentation of a hash table is implementation dependent so it may appear differently in your REPL.)
To get a value by a key from the hash table you use the gethash
function:
*h* ; => #<HASH-TABLE :TEST EQL :COUNT 3 {10024AD2B3}>`
(gethash :foo *h*) ; => :bar
; T
gethash
returns [multiple values][concepts-multiple-values] which will be explained in another concept.
To insert a value into a hash table you use setf
with gethash
like this:
(setf (gethash :foo *hash-table*) :bar) : => :bar
This will modify the value for the key :foo
in the hash table *hash-table*
to be :bar
.
It returns the value.
The use of setf
will be explained in another concept.
Lamont is a Lisp alien that wants to write their own video game. One task they have asked for your help with is keeping track of a high score list.
Define a function make-high-score-table
which does not take any arguments are evaluates to a new empty hash table:
(make-high-score-table) ; => #<HASH-TABLE :TEST EQL :COUNT 0 {1005A90103}>
To add a player to the table define a function add-player
which takes two arguments:
Set the player's initial score to zero.
(add-player hash-table :louis) ; => 0
(add-player hash-table :lilly) ; => 0
To set the score for a player define a function set-score
which takes three arguments:
(set-score hash-table :louis 1234) ; => 1234
To get a score for a player from the table define a function get-score
which takes two arguments:
This function should evaluate to the score stored for the player.
If there is no score stored for that player, or that player is not yet in the table then evaluate to 0
.
(get-score hash-table :louis) ; => 1234
(get-score hash-table :lilly) ; => 0
(get-score hash-table :not-a-character-in-this-story) ; => 0
To remove a player from the high score table define a function remove-player
which takes two arguments:
This function should gracefully handle cases where the player was not yet added to the table. Obviously after a player is removed if one gets their score from the table it will be zero.
(set-score hash-table :louis 1234) ; => 1234
(remove-player hash-table :louis) ; => T
(get-score hash-table :louis) ; => 0
(remove-player hash-table :not-in-the-table) ; => NIL
Sign up to Exercism to learn and master Common Lisp with 27 concepts, 83 exercises, and real human mentoring, all for free.