Association Lists, Property Lists and Hash Tables
Small maps, easy non-destructive extension, environments in interpreters
Keyword-style options, symbol properties Large mutable maps persistent map (Clojure, Racket immutable hash) Immutable data with efficient updates
;; Association list (alist): a list of pairs
(define phone-book '((ana . "555-1234") (bo . "555-9876")))
(assq 'ana phone-book) ; (ana . "555-1234")
(cdr (assq 'bo phone-book)) ; "555-9876"
(define updated (cons '(cy . "555-0000") phone-book)) ; non-destructive "update"
;; Hash tables (R7RS-large/SRFI-69, Racket, Guile)
(define h (make-hash)) ; Racket
(hash-set! h 'ana 30)
(hash-ref h 'ana) ; 30
(hash-ref h 'zed 0) ; 0 (default)
(hash-update! h 'ana add1)
(hash->list h);; Common Lisp
(defparameter *plist* '(:name "Ana" :age 30)) ; property list
(getf *plist* :age) ; 30
(defparameter *ht* (make-hash-table :test 'equal))
(setf (gethash "ana" *ht*) 30)
(gethash "ana" *ht*) ; 30, T;; Clojure: maps are literals and immutable
(def person {:name "Ana" :age 30})
(get person :age) ; 30
(:name person) ; "Ana"
(assoc person :email "[email protected]") ; new map
(update person :age inc) ; {:name "Ana", :age 31}
(dissoc person :age)
(merge person {:city "Pune"})
#{1 2 3} ; set literal
[1 2 3] ; vector literal