4 ms·
It's not that tough to make a literal syntax for hash tables with a simple reader macro. This may not be the best way to do it, I'm rather new to lisp, but it o
by twopoint718 18y ago
It's not that tough to make a literal syntax for hash tables with a simple reader macro. This may not be the best way to do it, I'm rather new to lisp, but it only took me about 5 min. (and a peek at "ANSI Common Lisp" 14.3, thanks, PG!):
(defun ins-hash (h vals)
(if (oddp (length vals))
nil
(if (not (null vals))
(progn
(setf (gethash (first vals) h) (second vals))
(ins-hash h (cddr vals)))
h)))
(set-macro-character #\} (get-macro-character #\)))
(set-dispatch-macro-character #\# #\{
#'(lambda (stream char1 char2)
(let ((items (read-delimited-list #\} stream t)))
(ins-hash (make-hash-table) items))))
CL-USER> (gethash :b #{:a 1 :b 2 :c 3})
2
T