Wave 4: add sequences/vectors/files chapters, 23 exercises, quiz3, finish ceremony, PCL table
This commit is contained in:
1 parent
77843402fa
commit
4675bd1106
76 files changed
+2505
-14
No files matched your search
@@ -0,0 +1,34 @@
|
||||
;;; 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))))
|
||||
Reference in new issue
Block a user