Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have hash tables in lisp?

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.

like image 446
Juanjo Conti Avatar asked Dec 03 '09 03:12

Juanjo Conti


2 Answers

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.

like image 195
Rainer Joswig Avatar answered Sep 28 '22 10:09

Rainer Joswig


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.

like image 33
seh Avatar answered Sep 28 '22 10:09

seh