Shammer's Philosophy

My private adversaria

Lisp の Hash Table

Hashtable の作成自体は、make-hash-table関数を使用する。以下、使用例。

> (setf car-hashtable (make-hash-table))
#<HASH-TABLE :TEST EQL size 0/60 #xC57544E>
> (gethash 'car car-hashtable)
NIL
NIL
> (setf (gethash 'my-car car-hashtable) 'Porte)
PORTE
> (setf (gethash 'my-want-car car-hashtable) 'AlfaRomeo)
ALFAROMEO
> (gethash 'my-car car-hashtable)
PORTE
T
> (gethash 'my-want-car car-hashtable)
ALFAROMEO
T
> (setf (gethash 'my-want-car car-hashtable) nil)
NIL
> (gethash 'my-want-car car-hashtable)
NIL
T
> (remhash 'my-want-car car-hashtable)
T
> (gethash 'my-want-car car-hashtable)
NIL
NIL
> (setf (gethash 'my-car car-hashtable) 'AlfaRomeo)
ALFAROMEO
> (gethash 'my-car car-hashtable)
ALFAROMEO
T

make-hash-tableでハッシュテーブルを作成して、setfとgethashを組み合わせてデータを追加していく。削除する場合は、NILをセットするだけでは、NILという値を保持するキーが残ったままになる。キーごと消すには、remhash関数を使う。