Can you have hash tables in lisp?

Can you have hash tables or dicts in Lisp? I mean a data structure, which is a set of pairs (key, value), where values ​​can be attached using keys.

+6
dictionary data-structures lisp hash
source share
7 answers

Generic Lisp have at least four different ways to do this (storing key values):

  • property lists (: foo 1: bar 2)
  • list of associations ((: foo. 1) (: bar. 2))
  • hash tables
  • CLOS objects (bar-value foo 'bar) for receiving and (setf (slot-value foo' bar) 42) for setting. The slot name can be stored in a variable: (let ((name 'bar)) (flag name fot)).

For simple use lists or property lists in order. With a lot of elements, they tend to become "slow." Hash tables are faster, but have their own tradeoffs. CLOS objects are used, as in many other object systems. The keys are the slot names defined in the CLOS class. Although you can program options that can add and remove slots at access.

+11
source share

If you reference Common Lisp hash tables , you get a hash-table .

Using these tables involves creating with the make-hash-table function, counting the gethash values, setting them, using gethash as place in conjunction with setf and deleting the entries from remhash .

Mapping from the key value to the hash code is available outside the hash tables using the sxhash function.

+9
source share

Of course - Common Lisp has hash tables .

 (setq a (make-hash-table)) (setf (gethash 'color a) 'brown) (setf (gethash 'name a) 'fred) (gethash 'color a) => brown (gethash 'name a) => fred (gethash 'pointy a) => nil 

Property lists are good for very small examples of demonstrative purpose, but for any real need, their performance is terrible, so use hash tables.

+7
source share

Clojure has a built-in card type:

 user=> (def m {:foo "bar" :baz "bla"}) #'user/m user=> (m :foo) "bar" 

See http://clojure.org/data_structures

+5
source share

Of course. Here's the SRFI that defines the standard hash table libraries in the Schema:

http://srfi.schemers.org/srfi-69/srfi-69.html

+2
source share

It has built- in ones that use a system hash function (usually SXHASH) and where you can have a couple of different checker equalities (EQ, EQL, EQUAL or EQUALP depending on what you consider to be "the same").

If the built-in hash tables are not good enough, there is also a common hash table . It will take any pair of “hash generator” / “key comparator” and build a hash table for you. However, it relies on a good hash function, and this is not necessarily trivial to write.

+1
source share

In Lisp, it is usually called a property list.

0
source share

All Articles