;;; HASH TABLES 4: MAPHASH (solution) (defparameter *stock* (let ((table (make-hash-table))) (setf (gethash 'tea table) 2 (gethash 'coffee table) 3 (gethash 'cocoa table) 4) table)) (defun stock-total () (let ((total 0)) (maphash (lambda (key value) (incf total value)) *stock*) total)) (defun keys-of (table) (let ((keys '())) (maphash (lambda (key value) (push key keys)) table) keys)) ;;; ------------------------------------------------------------------ ;;; Checks. Read them - they tell you what to aim for - but leave ;;; them unchanged. ;;; ------------------------------------------------------------------ (check-equal 9 (stock-total)) (check (let ((keys (keys-of *stock*))) (and (= 3 (length keys)) (member 'tea keys) (member 'coffee keys) (member 'cocoa keys))))