On this page:
4.1 Sets
4.1.1 Set Constructors
set
empty-set
list->set
custom-set
4.1.2 Set Accessors
set-contains?
set-empty?
set-count
set=?
subset?
proper-subset?
set->list
in-set
4.1.3 Set Updaters
set-insert
set-remove
set-insert!
set-remove!
set-union
set-intersection
set-difference
set-exclusive-or
4.1.4 Set Predicates
set?
set-can-insert?
set-can-remove?
set-can-insert!?
set-can-remove!?
4.1.5 Structures as Sets
prop: set
4.2 Dictionaries
4.2.1 Dictionary Constructors
empty-dict
make-dict
custom-dict
4.2.2 Dictionary Lookup
dict-ref!
dict-ref/ check
dict-ref/ identity
dict-ref/ default
dict-ref/ failure
4.2.3 Dictionary Accessors
dict-empty?
dict-has-key?
dict-domain
dict-range
4.2.4 Dictionary Combinations
dict-union
dict-union!
4.2.5 Dictionary Structure Properties
wrapped-dict-property
4.2.6 Contracted Dictionaries
4.3 Hash Tables
4.3.1 Hash Table Construction
hash
hash!
4.3.2 Hash Table Lookup
hash-ref/ check
hash-ref/ identity
hash-ref/ default
hash-ref/ failure
4.3.3 Hash Table Accessors
hash-equal?
hash-has-key?
hash-domain
hash-range
4.3.4 Hash Table Combinations
hash-union
hash-union!
4.4 Imperative Queues
make-queue
enqueue!
dequeue!
queue-empty?
queue?
queue/ c
nonempty-queue/ c
Version: 5.1.1.2

4 Collection Datatypes

4.1 Sets

 (require (planet cce/scheme:7:4/set))

This module provides tools for representing finite sets.

4.1.1 Set Constructors

(set [#:mutable? mutable?    
  #:weak? weak?    
  #:compare compare]    
  x ...)  set?
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  compare : (or/c 'eq 'eqv 'equal) = 'equal
  x : any/c
Produces a hash table-based set using the hash table properties described by any keyword arguments, and the given values as elements of the set.

Examples:

> (set 1 2 3)

'#hash((1)      (2)      (3))

> (set #:mutable? #t 1 2 3)

'#hash((1)      (2)      (3))

> (set #:weak? #t 1 2 3)

'#hash((1)      (2)      (3))

> (set #:compare 'eqv 1 2 3)

'#hasheqv((1)      (2)      (3))

(empty-set [#:mutable? mutable?    
  #:weak? weak?    
  #:compare compare])  set?
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  compare : (or/c 'eq 'eqv 'equal) = 'equal
Produces an empty hash table-based set using the hash table properties described by any keyword arguments.

Examples:

> (empty-set)

'#hash()

> (empty-set #:mutable? #t)

'#hash()

> (empty-set #:weak? #t)

'#hash()

> (empty-set #:compare 'eqv)

'#hasheqv()

(list->set [#:mutable? mutable?    
  #:weak? weak?    
  #:compare compare]    
  lst)  set?
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  compare : (or/c 'eq 'eqv 'equal) = 'equal
  lst : list?
Produces a hash table-based set using the hash table properties described by any keyword arguments, with the elements of the given list as the elements of the set.

Examples:

> (list->set '(1 2 3))

'#hash((1)      (2)      (3))

> (list->set #:mutable? #t '(1 2 3))

'#hash((1)      (2)      (3))

> (list->set #:weak? #t '(1 2 3))

'#hash((1)      (2)      (3))

> (list->set #:compare 'eqv '(1 2 3))

'#hasheqv((1)      (2)      (3))

(custom-set #:compare compare    
  [#:hash hash    
  #:hash2 hash2    
  #:mutable? mutable?    
  #:weak? weak?]    
  elem ...)  set?
  compare : (-> any/c any/c any/c)
  hash : (-> any/c exact-integer?) = (lambda (x) 0)
  hash2 : (-> any/c exact-integer?) = (lambda (x) 0)
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  elem : any/c
Produces a custom hash table-based set using the given equality predicate equiv? and optional hash functions hash-primary and hash-secondary. If no hash functions are given, they default to a degenerate hash function, resulting in an effectively list-based set. The set is populated with the given elem values.

Examples:

(define singularity
  (custom-set 'one 'two 'three
              #:mutable? #t
              #:compare (lambda (a b) #t)))
> (set->list singularity)

'(one)

> (set-insert! singularity 'four)
> (set->list singularity)

'(one)

> (set-remove! singularity 'zero)
> (set->list singularity)

'()

4.1.2 Set Accessors

(set-contains? s x)  boolean?
  s : set?
  x : any/c
Reports whether s contains x.

Examples:

> (set-contains? (set 1 2 3) 1)

#t

> (set-contains? (set 1 2 3) 4)

#f

(set-empty? s)  boolean?
  s : set?
Reports whether a set is empty.

Examples:

> (set-empty? '())

#t

> (set-empty? '((1 . one)))

#f

Reports the number of elements in s.

Examples:

> (set-count (set))

0

> (set-count (set 1 2 3))

3

(set=? a b)  boolean?
  a : set?
  b : set?
Reports whether two sets contain the same elements.

Examples:

> (set=? (set 1) (set 1 2 3))

#f

> (set=? (set 1 2 3) (set 1))

#f

> (set=? (set 1 2 3) (set 1 2 3))

#t

(subset? a b)  boolean?
  a : set?
  b : set?
Reports whether b contains all of the elements of a.

Examples:

> (subset? (set 1) (set 1 2 3))

#t

> (subset? (set 1 2 3) (set 1))

#f

> (subset? (set 1 2 3) (set 1 2 3))

#t

(proper-subset? a b)  boolean?
  a : set?
  b : set?
Reports whether b contains all of the elements of a, and at least one element not in a.

Examples:

> (proper-subset? (set 1) (set 1 2 3))

#t

> (proper-subset? (set 1 2 3) (set 1))

#f

> (proper-subset? (set 1 2 3) (set 1 2 3))

#f

(set->list s)  list?
  s : set?
Produces a list containing the elements of s.

Example:

> (set->list (set 1 2 3))

'(1 2 3)

(in-set s)  sequence?
  s : set?
Produces a sequence iterating over the elements of the set.

Example:

> (for/list ([x (in-set (set 1 2 3))]) x)

'(1 2 3)

4.1.3 Set Updaters

(set-insert s x)  set?
  s : set?
  x : any/c
Produces a new version of s containing x.

Examples:

> (set-insert (set 1 2 3) 4)

'#hash((1)      (2)      (3)      (4))

> (set-insert (set 1 2 3) 1)

'#hash((1)      (2)      (3))

(set-remove s x)  set?
  s : set?
  x : any/c
Produces a new version of s that does not contain x.

Examples:

> (set-remove (set 1 2 3) 1)

'#hash((2)      (3))

> (set-remove (set 1 2 3) 4)

'#hash((1)      (2)      (3))

(set-insert! s x)  void?
  s : set?
  x : any/c
Mutates s to contain x.

Examples:

(define s (set #:mutable? #t 1 2 3))
> s

'#hash((1)      (2)      (3))

> (set-insert! s 4)
> s

'#hash((1)      (2)      (3)      (4))

> (set-insert! s 1)
> s

'#hash((1)      (2)      (3)      (4))

(set-remove! s x)  void?
  s : set?
  x : any/c
Mutates x so as not to contain x.

Examples:

(define s (set #:mutable? #t 1 2 3))
> s

'#hash((1)      (2)      (3))

> (set-remove! s 1)
> s

'#hash((2)      (3))

> (set-remove! s 4)
> s

'#hash((2)      (3))

(set-union s0 s ...)  set?
  s0 : (and/c set? set-can-insert?)
  s : set?
Produces a new version of s0 containing all the elements in each s.

Example:

> (set-union (set 1 2) (set 1 3) (set 2 3))

'#hash((1)      (2)      (3))

(set-intersection s0 s ...)  set?
  s0 : (and/c set? set-can-remove?)
  s : set?
Produces a new version of s0 containing only those elements found in every s.

Example:

> (set-intersection (set 1 2 3) (set 1 2) (set 2 3))

'#hash((2))

(set-difference s0 s ...)  set?
  s0 : (and/c set? set-can-remove?)
  s : set?
Produces a new version of s0 containing only those elements not found in any s.

Example:

> (set-difference (set 1 2 3) (set 1) (set 3))

'#hash((2))

(set-exclusive-or s0 s ...)  set?
  s0 : (and/c set? set-can-insert? set-can-remove?)
  s : set?
Produces a new version of s0 containing only those elements found in s0 and each s an odd number of times.

Example:

> (set-exclusive-or (set 1) (set 1 2) (set 1 2 3))

'#hash((1)      (3))

4.1.4 Set Predicates

(set? x)  boolean?
  x : any/c
Recognizes sets. A set is either a dictionary or a structure with the prop:set property.

Examples:

> (set? '(1 2))

#f

> (set? '((1 . one) (2 . two)))

#t

(set-can-insert? s)  boolean?
  s : set?
(set-can-remove? s)  boolean?
  s : set?
(set-can-insert!? s)  boolean?
  s : set?
(set-can-remove!? s)  boolean?
  s : set?
Report whether s supports set-insert, set-remove, set-insert!, or set-remove!, respectively.

Examples:

(define functional-set (set 1 2 3))
> (set-can-insert? functional-set)

#t

> (set-can-remove? functional-set)

#t

> (set-can-insert!? functional-set)

#f

> (set-can-remove!? functional-set)

#f

(define imperative-set (set #:mutable? #t 1 2 3))
> (set-can-insert? imperative-set)

#f

> (set-can-remove? imperative-set)

#f

> (set-can-insert!? imperative-set)

#t

> (set-can-remove!? imperative-set)

#t

4.1.5 Structures as Sets

Property for structurs as sets. Its value must be a vector of 7 elements, as follows:

Examples:

(define (never-contains? set elem) #f)
(define (never-insert! set elem) (error 'set-insert! "always empty!"))
(define (never-insert set elem) (error 'set-insert "always empty!"))
(define (never-remove! set elem) (void))
(define (never-remove set elem) set)
(define (always-zero set) 0)
(define (no-elements set) null)
(define-struct always-empty []
  #:transparent
  #:property prop:set
  (vector never-contains?
          never-insert!
          never-insert
          never-remove!
          never-remove
          always-zero
          no-elements))
> (set? (make-always-empty))

#t

> (set-contains? (make-always-empty) 1)

#f

> (set-insert! (make-always-empty) 2)

set-insert!: always empty!

> (set-insert (make-always-empty) 3)

set-insert: always empty!

> (set-remove (make-always-empty) 4)

(always-empty )

> (set-remove! (make-always-empty) 5)
> (set-count (make-always-empty))

0

> (for ([x (in-set (make-always-empty))])
    (printf "~s\n" x))

4.2 Dictionaries

 (require (planet cce/scheme:7:4/dict))

This module provides tools for manipulating dictionary values.

4.2.1 Dictionary Constructors

(empty-dict [#:mutable? mutable?    
  #:weak? weak?    
  #:compare compare])  hash?
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  compare : (or/c 'eq 'eqv 'equal) = equal
Constructs an empty hash table based on the behavior specified by mutable?, weak?, and compare.

Examples:

> (empty-dict)

'#hash()

> (empty-dict #:mutable? #t)

'#hash()

> (empty-dict #:weak? #t)

'#hash()

> (empty-dict #:compare 'eqv)

'#hasheqv()

(make-dict d    
  [#:mutable? mutable?    
  #:weak? weak?    
  #:compare compare])  hash?
  d : dict?
  mutable? : boolean? = weak?
  weak? : boolean? = #f
  compare : (or/c 'eq 'eqv 'equal) = equal
Converts a given dictionary d to a hash table based on the behavior specified by mutable?, weak?, and compare.

Examples:

> (make-dict '([1 . one] [2 . two]))

'#hash((1 . one) (2 . two))

> (make-dict '([1 . one] [2 . two]) #:mutable? #t)

'#hash((1 . one) (2 . two))

> (make-dict '([1 . one] [2 . two]) #:weak? #t)

'#hash((1 . one) (2 . two))

> (make-dict '([1 . one] [2 . two]) #:compare 'eqv)

'#hasheqv((1 . one) (2 . two))

(custom-dict equiv?    
  [hash-primary    
  hash-secondary    
  #:mutable? mutable?    
  #:weak? weak?])  dict?
  equiv? : (-> any/c any/c any/c)
  hash-primary : (-> any/c exact-integer?) = (lambda (x) 0)
  hash-secondary : (-> any/c exact-integer?) = (lambda (x) 0)
  mutable? : boolean? = weak?
  weak? : boolean? = #f
Constructs a dictionary based on custom comparison and optional hash functions. Given no hash functions, the dictionary defaults to a degenerate hash function and is thus essentially equivalent to a list-based dictionary.

Examples:

(define table (custom-dict = add1 sub1 #:mutable? #t))
> (dict-set! table 1 'one)
> (dict-set! table 2 'two)
> (for/list ([(key val) (in-dict table)])
    (cons key val))

'((2 . two) (1 . one))

4.2.2 Dictionary Lookup

(dict-ref! d k v)  any/c
  d : (and/c dict? dict-mutable?)
  k : any/c
  v : (or/c (-> any/c) any/c)
Looks up key k in dictionary d. If d has no entry for k, updates d to map k to the result of (v) (if v is a procedure) or v (otherwise), and returns the new mapping.

Examples:

(define d (make-hash))
> (dict-set! d 1 'one)
> (dict-set! d 2 'two)
> d

'#hash((1 . one) (2 . two))

> (dict-ref! d 2 'dos)

'two

> d

'#hash((1 . one) (2 . two))

> (dict-ref! d 3 'tres)

'tres

> d

'#hash((1 . one) (2 . two) (3 . tres))

> (dict-ref! d 4 gensym)

'g2511

> d

'#hash((1 . one) (2 . two) (3 . tres) (4 . g2511))

(dict-ref/check d k)  any/c
  d : dict?
  k : (lambda (k) (dict-has-key? d k))
Looks up key k in dictionary d. Raises a contract error if d has no entry for k. Equivalent to (dict-ref d k), except for the specific exception value raised.

Example:

> (dict-ref/check '([1 . one] [2 . two] [3 . three]) 2)

'two

(dict-ref/identity d k)  any/c
  d : dict?
  k : any/c
Looks up key k in dictionary d. Returns k if d has no entry for k. Equivalent to (dict-ref d k (lambda () k)).

Examples:

> (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 2)

'two

> (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 4)

4

(dict-ref/default d k v)  any/c
  d : dict?
  k : any/c
  v : any/c
Looks up key k in dictionary d. Returns v if d has no entry for k. Equivalent to (dict-ref d k (lambda () v)).

Examples:

> (dict-ref/default '([1 . one] [2 . two] [3 . three]) 2 'other)

'two

> (dict-ref/default '([1 . one] [2 . two] [3 . three]) 4 'other)

'other

(dict-ref/failure d k f)  any/c
  d : dict?
  k : any/c
  f : (-> any/c)
Looks up key k in dictionary d. Returns the result of applying f (in tail position) if d has no entry for k. Equivalent to (dict-ref d k f).

Examples:

> (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 2 gensym)

'two

> (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 4 gensym)

'g2657

4.2.3 Dictionary Accessors

(dict-empty? d)  boolean?
  d : dict?
Reports whether d is empty (has no keys).

Examples:

> (dict-empty? '())

#t

> (dict-empty? '([1 . one] [2 . two]))

#f

(dict-has-key? d k)  boolean?
  d : dict?
  k : any/c
Reports whether d has an entry for k.

Examples:

> (dict-has-key? '([1 . one] [2 . two] [3 . three]) 2)

#t

> (dict-has-key? '([1 . one] [2 . two] [3 . three]) 4)

#f

(dict-domain d)  list?
  d : dict?
Produces the domain of a dictionary as a list of keys.

Example:

> (dict-domain '([1 . one] [2 . two] [3 . three]))

'(1 2 3)

(dict-range d)  list?
  d : dict?
Produces the range of a dictionary as a list of values.

Example:

> (dict-range '([1 . one] [2 . two] [3 . three]))

'(one two three)

4.2.4 Dictionary Combinations

(dict-union d0 
  d ... 
  [#:combine combine 
  #:combine/key combine/key]) 
  (and/c dict? dict-can-functional-set?)
  d0 : (and/c dict? dict-can-functional-set?)
  d : dict?
  combine : (-> any/c any/c any/c)
   = (lambda _ (error 'dict-union ...))
  combine/key : (-> any/c any/c any/c any/c)
   = (lambda (k a b) (combine a b))
Computes the union of d0 with each dictionary d by functional update, adding each element of each d to d0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:

> (dict-union '([1 . one]) '([2 . two]) '([3 . three]))

'((1 . one) (2 . two) (3 . three))

> (dict-union '([1    one uno]  [2    two dos])
              '([1    ein une]  [2    zwei deux])
              #:combine/key (lambda (k v1 v2) (append v1 v2)))

'((1 one uno ein une) (2 two dos zwei deux))

(dict-union! d0    
  d ...    
  [#:combine combine    
  #:combine/key combine/key])  void?
  d0 : (and/c dict? dict-mutable?)
  d : dict?
  combine : (-> any/c any/c any/c)
   = (lambda _ (error 'dict-union! ...))
  combine/key : (-> any/c any/c any/c any/c)
   = (lambda (k a b) (combine a b))
Computes the union of d0 with each dictionary d by mutable update, adding each element of each d to d0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:

(define d (make-hash))
> d

'#hash()

> (dict-union! d '([1    one uno]  [2    two dos]))
> d

'#hash((1 . (one uno)) (2 . (two dos)))

> (dict-union! d
               '([1    ein une]  [2    zwei deux])
               #:combine/key (lambda (k v1 v2) (append v1 v2)))
> d

'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux)))

4.2.5 Dictionary Structure Properties

(wrapped-dict-property #:unwrap unwrap    
  [#:wrap wrap    
  #:predicate pred    
  #:mutable? mutable?    
  #:weak? mutable?    
  #:functional? functional?])  vector?
  unwrap : (-> (and/c dict? pred) dict?)
  wrap : (-> dict? (and/c dict? pred)) = (lambda (x) x)
  pred : (-> any/c boolean?) = (lambda (x) #t)
  mutable? : boolean? = weak?
  mutable? : boolean? = #f
  functional? : boolean? = #t
Produces a value appropriate for prop:dict for a derived dictionary type recognized by pred. Dictionaries constructed from this property will extract a nested dictionary using unwrap and will produce a wrapped dictionary during functional update using wrap.

Examples:

(define-struct table [dict]
  #:transparent
  #:property prop:dict
  (wrapped-dict-property
   #:unwrap (lambda (d) (table-dict d))
   #:wrap (lambda (d) (make-table d))
   #:predicate (lambda (d) (table? d))))
> (dict? (make-table '([1 . one] [2 . two])))

#t

> (dict-ref (make-table '([1 . one] [2 . two])) 1)

'one

> (dict-set (make-table '([1 . one] [2 . two])) 3 'three)

(table '((1 . one) (2 . two) (3 . three)))

4.2.6 Contracted Dictionaries

This library re-provides dict/c from (planet cce/scheme:7:4/contract).

4.3 Hash Tables

 (require (planet cce/scheme:7:4/hash))

This module provides tools for manipulating hash tables.

4.3.1 Hash Table Construction

(hash immutable-hash-type [key-expr value-expr] ...)
 
immutable-hash-type = 
  | #:eq
  | #:eqv
  | #:equal
Produces an immutable hash table based on the given comparison, defaulting to #:equal, and mapping the result of each key-expr to the result of each value-expr.

Examples:

> (hash ['one 1] ['two 2])

'#hash((one . 1) (two . 2))

> (hash #:eq ['one 1] ['two 2])

'#hasheq((one . 1) (two . 2))

> (hash #:eqv ['one 1] ['two 2])

'#hasheqv((one . 1) (two . 2))

> (hash #:equal ['one 1] ['two 2])

'#hash((one . 1) (two . 2))

(hash! mutable-hash-spec [key-expr value-expr] ...)
 
mutable-hash-spec = mutable-hash-type mutable-hash-weak
  | mutable-hash-weak mutable-hash-type
     
mutable-hash-type = 
  | #:eq
  | #:eqv
  | #:equal
     
mutable-hash-weak = 
  | #:weak
Produces a mutable hash table based on the given comparison and weakness specification, defaulting to #:equal and not #:weak, and mapping the result of each key-expr to the result of each value-expr.

Examples:

> (hash! ['one 1] ['two 2])

'#hash((one . 1) (two . 2))

> (hash! #:eq ['one 1] ['two 2])

'#hasheq((one . 1) (two . 2))

> (hash! #:eqv #:weak ['one 1] ['two 2])

'#hasheqv((one . 1) (two . 2))

> (hash! #:weak #:equal ['one 1] ['two 2])

'#hash((one . 1) (two . 2))

4.3.2 Hash Table Lookup

(hash-ref/check h k)  any/c
  h : hash?
  k : (lambda (k) (hash-has-key? h k))
Looks up key k in hash table h. Raises a contract error if h has no entry for k. Equivalent to (hash-ref h k), except for the specific exception value raised.

Example:

> (hash-ref/check (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)

'two

(hash-ref/identity h k)  any/c
  h : hash?
  k : any/c
Looks up key k in hash table h. Returns k if h has no entry for k. Equivalent to (hash-ref h k (lambda () k)).

Examples:

> (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)

'two

> (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4)

4

(hash-ref/default h k v)  any/c
  h : hash?
  k : any/c
  v : any/c
Looks up key k in hash table h. Returns v if h has no entry for k. Equivalent to (hash-ref h k (lambda () v)).

Examples:

> (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 'other)

'two

> (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 'other)

'other

(hash-ref/failure h k f)  any/c
  h : hash?
  k : any/c
  f : (-> any/c)
Looks up key k in hash table h. Returns the result of applying f (in tail position) if h has no entry for k. Equivalent to (hash-ref h k f).

Examples:

> (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 gensym)

'two

> (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 gensym)

'g2139

4.3.3 Hash Table Accessors

(hash-equal? h)  boolean?
  h : hash?
Reports whether h maps keys according to equal?.

Examples:

> (hash-equal? #hash())

#t

> (hash-equal? #hasheq())

#f

> (hash-equal? #hasheqv())

#f

(hash-has-key? h k)  boolean?
  h : hash?
  k : any/c
Reports whether h has an entry for k. This function is re-exported from scheme/base. In versions of PLT Scheme before hash-has-key? was implemented, this module provides its own definition.

Examples:

> (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2)

#t

> (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4)

#f

(hash-domain h)  list?
  h : hash?
Produces the domain of a hash table as a list of keys.

Example:

> (hash-domain (make-immutable-hash '([1 . one] [2 . two] [3 . three])))

'(1 2 3)

(hash-range h)  list?
  h : hash?
Produces the range of a hash table as a list of values.

Example:

> (hash-range (make-immutable-hash '([1 . one] [2 . two] [3 . three])))

'(one two three)

4.3.4 Hash Table Combinations

(hash-union h0 
  h ... 
  [#:combine combine 
  #:combine/key combine/key]) 
  (and/c hash? hash-can-functional-set?)
  h0 : (and/c hash? hash-can-functional-set?)
  h : hash?
  combine : (-> any/c any/c any/c)
   = (lambda _ (error 'hash-union ...))
  combine/key : (-> any/c any/c any/c any/c)
   = (lambda (k a b) (combine a b))
Computes the union of h0 with each hash table h by functional update, adding each element of each h to h0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:

> (hash-union (make-immutable-hash '([1 . one])) (make-immutable-hash '([2 . two])) (make-immutable-hash '([3 . three])))

'#hash((1 . one) (2 . two) (3 . three))

> (hash-union (make-immutable-hash '([1    one uno]  [2    two dos]))
              (make-immutable-hash '([1    ein une]  [2    zwei deux]))
              #:combine/key (lambda (k v1 v2) (append v1 v2)))

'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux)))

(hash-union! h0    
  h ...    
  [#:combine combine    
  #:combine/key combine/key])  void?
  h0 : (and/c hash? hash-mutable?)
  h : hash?
  combine : (-> any/c any/c any/c)
   = (lambda _ (error 'hash-union ...))
  combine/key : (-> any/c any/c any/c any/c)
   = (lambda (k a b) (combine a b))
Computes the union of h0 with each hash table h by mutable update, adding each element of each h to h0 in turn. For each key k and value v, if a mapping from k to some value v0 already exists, it is replaced with a mapping from k to (combine/key k v0 v).

Examples:

(define h (make-hash))
> h

'#hash()

> (hash-union! h (make-immutable-hash '([1    one uno]  [2    two dos])))
> h

'#hash((1 . (one uno)) (2 . (two dos)))

> (hash-union! h
               (make-immutable-hash '([1    ein une]  [2    zwei deux]))
               #:combine/key (lambda (k v1 v2) (append v1 v2)))
> h

'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux)))

4.4 Imperative Queues

 (require (planet cce/scheme:7:4/queue))

This module provides a mutable queue representation.

Produces an empty queue.

(enqueue! q v)  void?
  q : queue/c
  v : any/c
Adds an element to the back of a queue.

(dequeue! q)  any/c
  q : nonempty-queue/c
Removes an element from the front of a nonempty queue, and returns that element.

Examples:

(define q (make-queue))
> (enqueue! q 1)
> (dequeue! q)

1

> (enqueue! q 2)
> (enqueue! q 3)
> (dequeue! q)

2

> (dequeue! q)

3

(queue-empty? q)  boolean?
  q : queue/c
Recognizes whether a queue is empty or not.

Examples:

(define q (make-queue))
> (queue-empty? q)

#t

> (enqueue! q 1)
> (queue-empty? q)

#f

> (dequeue! q)

1

> (queue-empty? q)

#t

(queue? v)  boolean?
  v : any/c
This predicate recognizes queues.

Examples:

> (queue? (make-queue))

#t

> (queue? 'not-a-queue)

#f

These contracts recognize queues; the latter requires the queue to contain at least one value.