Hash tables

A data structure that is used occasionally in LilyPond. A hash table is similar to an array, but the indexes to the array can be any type of Scheme value, not just integers.

Hash tables are more efficient than alists if there is a lot of data to store and the data changes very infrequently.

The syntax to create hash tables is a bit complex, but you can see examples of it in the LilyPond source.

guile> (define h (make-hash-table 10))
guile> h
$15 = #<hash-table 1b9e0640 0/31>
guile> (hashq-set! h 'key1 "val1")
$16 = "val1"
guile> (hashq-set! h 'key2 "val2")
$17 "val2"
guile> (hashq-set! h 3 "val3")
$18 = "val3"
guile>

(The hexadecimal number 1b9e0640, uniquely identifying the hash, might be different on your computer. We don’t need this information here.)

Values are retrieved from hash tables with hashq-ref.

guile> (hashq-ref h 3)
$19 = "val3"
guile> (hashq-ref h 'key2)
$20 = "val2"
guile>

Keys and values are retrieved as a pair with hashq-get-handle. This is a preferred way, because it will return #f if a key is not found.

guile> (hashq-get-handle h 'key1)
$21 = (key1 . "val1")
guile> (hashq-get-handle h 'frob)
$22 = #f
guile>

Extending LilyPond v2.27.0 (development-branch).