The (ice-9 vlist) module provides an implementation of VList-based
hash lists (see VLists). VList-based hash lists, or vhashes, are an
immutable dictionary type similar to association lists that maps keys to
values. However, unlike association lists, accessing a value given its
key is typically a constant-time operation.
The VHash programming interface of (ice-9 vlist) is mostly the same as
that of association lists found in SRFI-1, with procedure names prefixed by
vhash- instead of vlist- (see SRFI-1 Association Lists).
In addition, vhashes can be manipulated using VList operations:
(vlist-head (vhash-consq 'a 1 vlist-null))
⇒ (a . 1)
(define vh1 (vhash-consq 'b 2 (vhash-consq 'a 1 vlist-null)))
(define vh2 (vhash-consq 'c 3 (vlist-tail vh1)))
(vhash-assq 'a vh2)
⇒ (a . 1)
(vhash-assq 'b vh2)
⇒ #f
(vhash-assq 'c vh2)
⇒ (c . 3)
(vlist->list vh2)
⇒ ((c . 3) (a . 1))
However, keep in mind that procedures that construct new VLists
(vlist-map, vlist-filter, etc.) return raw VLists, not vhashes:
(define vh (alist->vhash '((a . 1) (b . 2) (c . 3)) hashq))
(vhash-assq 'a vh)
⇒ (a . 1)
(define vl
;; This will create a raw vlist.
(vlist-filter (lambda (key+value) (odd? (cdr key+value))) vh))
(vhash-assq 'a vl)
⇒ ERROR: Wrong type argument in position 2
(vlist->list vl)
⇒ ((a . 1) (c . 3))
Return a new hash list based on vhash where key is associated with value, using hash-proc to compute the hash of key. vhash must be either
vlist-nullor a vhash returned by a previous call tovhash-cons. hash-proc defaults tohash(seehashprocedure). Withvhash-consq, thehashqhash function is used; withvhash-consvthehashvhash function is used.All
vhash-conscalls made to construct a vhash should use the same hash-proc. Failing to do that, the result is undefined.
Return the first key/value pair from vhash whose key is equal to key according to the equal? equality predicate (which defaults to
equal?), and using hash-proc (which defaults tohash) to compute the hash of key. The second form useseq?as the equality predicate andhashqas the hash function; the last form useseqv?andhashv.Note that it is important to consistently use the same hash function for hash-proc as was passed to
vhash-cons. Failing to do that, the result is unpredictable.
Remove all associations from vhash with key, comparing keys with equal? (which defaults to
equal?), and computing the hash of key using hash-proc (which defaults tohash). The second form useseq?as the equality predicate andhashqas the hash function; the last one useseqv?andhashv.Again the choice of hash-proc must be consistent with previous calls to
vhash-cons.
Fold over the key/pair elements of vhash. For each pair call proc as
(prockey value result).
Fold over all the values associated with key in vhash, with each call to proc having the form
(proc value result), where result is the result of the previous call to proc and init the value of result for the first call to proc.Keys in vhash are hashed using hash are compared using equal?. The second form uses
eq?as the equality predicate andhashqas the hash function; the third one useseqv?andhashv.Example:
(define vh (alist->vhash '((a . 1) (a . 2) (z . 0) (a . 3)))) (vhash-fold* cons '() 'a vh) ⇒ (3 2 1) (vhash-fold* cons '() 'z vh) ⇒ (0)