24 lines
830 B
Common Lisp
24 lines
830 B
Common Lisp
;;; HASH TABLES 3: COUNTING
|
|
;;;
|
|
;;; The classic hash-table job: tally. GETHASH takes a default:
|
|
;;; (gethash x table 0)
|
|
;;; so a missing key counts as 0 and INCF can do the rest.
|
|
;;; (hash-table-count table) is the number of stored keys.
|
|
|
|
;; TODO: Count the occurrences of each element of XS. Return the
|
|
;;; table.
|
|
(defun tally (xs)
|
|
(let ((table (make-hash-table)))
|
|
table))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (let ((table (tally '(a b a c a b))))
|
|
(and (eql 3 (gethash 'a table))
|
|
(eql 2 (gethash 'b table))
|
|
(eql 1 (gethash 'c table))
|
|
(eql 3 (hash-table-count table)))))
|