Files
lisplings/exercises/13_hash_tables/hash3.lisp
T
venomade 6e2c382c78 Fix 16 mid-chapter exercise defects (scouts mid1+mid2)
Verified: dev/verify.sh all 70 exercises green (broken fail, solved pass).
2026-11-22 04:52:35 +00:00

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)))))