DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(guile.info.gz) Symbol Keys

Info Catalog (guile.info.gz) Symbol Data (guile.info.gz) Symbols (guile.info.gz) Symbol Variables
 
 21.6.2 Symbols as Lookup Keys
 -----------------------------
 
 Given their efficiency and descriptive power, it is natural to use
 symbols as the keys in an association list or hash table.
 
    To illustrate this, consider a more structured representation of the
 car properties example from the preceding subsection.  Rather than
 mixing all the properties up together in a flat list, we could use an
 association list like this:
 
      (define car1-properties '((colour . red)
                                (transmission . manual)
                                (fuel . unleaded)
                                (steering . power-assisted)))
 
    Notice how this structure is more explicit and extensible than the
 flat list.  For example it makes clear that `manual' refers to the
 transmission rather than, say, the windows or the locking of the car.
 It also allows further properties to use the same symbols among their
 possible values without becoming ambiguous:
 
      (define car1-properties '((colour . red)
                                (transmission . manual)
                                (fuel . unleaded)
                                (steering . power-assisted)
                                (seat-colour . red)
                                (locking . manual)))
 
    With a representation like this, it is easy to use the efficient
 `assq-XXX' family of procedures ( Association Lists) to extract
 or change individual pieces of information:
 
      (assq-ref car1-properties 'fuel) => unleaded
      (assq-ref car1-properties 'transmission) => manual
 
      (assq-set! car1-properties 'seat-colour 'black)
      =>
      ((colour . red)
       (transmission . manual)
       (fuel . unleaded)
       (steering . power-assisted)
       (seat-colour . black)
       (locking . manual)))
 
    Hash tables also have keys, and exactly the same arguments apply to
 the use of symbols in hash tables as in association lists.  The hash
 value that Guile uses to decide where to add a symbol-keyed entry to a
 hash table can be obtained by calling the `symbol-hash' procedure:
 
  -- Scheme Procedure: symbol-hash symbol
  -- C Function: scm_symbol_hash (symbol)
      Return a hash value for SYMBOL.
 
    See  Hash Tables for information about hash tables in
 general, and for why you might choose to use a hash table rather than
 an association list.
 
Info Catalog (guile.info.gz) Symbol Data (guile.info.gz) Symbols (guile.info.gz) Symbol Variables
automatically generated byinfo2html