views:

215

answers:

7

Can you have hash tables or dicts in Lisp? I mean the data structure that is a collection of pairs (key, value) where values can be acceded using keys.

A: 

In Lisp it's usually called a property list.

Jerry Coffin
No, property lists are something different. See the CLHS glossary: http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_p.htm#property_list
Eli Barzilay
Yes and no -- a property list isn't a hash table, but it does provide a dictionary-like interface (and his question specifies "...the data structure that is a collection of pairs (key, value) where values can be acceded using keys." A property list certainly provides exactly that, though without hashing (or anything approaching the same performance...)
Jerry Coffin
+2  A: 

Sure. Here's the SRFI defining the standard hash table libraries in Scheme:

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

mquander
+6  A: 

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 abysmal, so use hash tables.

Eli Bendersky
+7  A: 

If you're referring to Common Lisp, hash tables are provided by a type called hash-table.

Using these tables involves creating one with function make-hash-table, reading values with gethash, setting them by using gethash as a place in concert with setf, and removing entries with remhash.

The mapping from key value to hash code is available outside of hash tables with the function sxhash.

seh
+4  A: 

Clojure has a built-in map type:

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

See http://clojure.org/data%5Fstructures

harto
+5  A: 

Common Lisp has at least four different ways to do that (key value storage):

  • property lists (:foo 1 :bar 2)
  • assoc lists ((:foo . 1) (:bar . 2))
  • hash tables
  • CLOS objects (slot-value foo 'bar) to get and (setf (slot-value foo 'bar) 42) to set. The slot name can be stored in a variable: (let ((name 'bar)) (slot-value foo name)) .

For simple usage assoc lists or property lists are fine. With a larger number of elements they tend to get 'slow'. Hash tables are 'faster' but have their own tradeoffs. CLOS objects are used like in many other object systems. The keys are the slot-names defined in a CLOS class. Though it is possible to program variants that can add and remove slots on access.

Rainer Joswig
+1  A: 

There's built-in hash tables, that use a system hash function (typically SXHASH) and where you can have a couple of different equality checkers (EQ, EQL, EQUAL or EQUALP depending on what you consider to be "the same" key).

If the built-in hash tables are not good enough, there's also a generic hash table library. It will accept any pair of "hash generator"/"key comparator" and build you a hash table. However, it relies on having a good hash function to work well and that is not necessarily trivial to write.

Vatine