guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hash tables


From: Martin Grabmueller
Subject: Re: hash tables
Date: Sat, 30 Jun 2001 08:46:18 +0200

> From: address@hidden (Thomas Bushnell, BSG)
> Sender: address@hidden
> 
> So what are the rules for simple hash tables in guile?

Hash table map objects to objects, so the keys and values can be of
any type. There are several types of functions, which differ in how
they compare keys for equality.  The procedures with `hashq' in the
name use eq?, procedures with `hashv' use eqv? and with `hash' use
equal?.  Additionally there are procedures whose names start with
`hashx', they accept the equality predicate as an additional argument.

> Importantly, how do I create a hash table?  What is the tester
> function to tell me if a given object is a hash table?

You can simply create them with `(make-vector size '())', since hash
tables are simply vectors of association lists, but there is also the
procedure `(make-hash-table size)', which you should probably use.

For testing if a given key is in a table, use
`hash{q,v,x,}-get-handle', which returns either a (key . value) pair
or #f, if the key is not in the table.

> Is there a length function, or do I have to use hash-fold and count
> them by hand?

No, you have to use `hash-fold'

guile> (define h (make-hash-table 310))
guile> (hashq-create-handle! h 'foo "bar")
(foo . "bar")
guile> (hashq-create-handle! h 'braz "zonk")
(braz . "zonk")
guile> (hashq-get-handle h 'foo)
(foo . "bar")
guile> (hashq-get-handle h 'lala)
#f
guile> (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
2

For additional information, see the CVS Guile Reference Manual, for
example at:

http://www.glug.org/docbits/guile-doc-out/html/guile-1.5.0/guile_9.html#SEC133

HTH,
  'martin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]