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,50 @@
|
||||
;;; QUIZ 3: THE LUNCH TALLY (solution)
|
||||
|
||||
(define-condition not-ordered (error)
|
||||
((item :initarg :item :reader not-ordered-item))
|
||||
(:documentation "Signaled when the kitchen is asked for an item nobody ordered."))
|
||||
|
||||
(defparameter *orders*
|
||||
'(soup bread soup bread soup))
|
||||
|
||||
(defparameter *menu*
|
||||
'((soup . 7) (bread . 3) (salad . 5)))
|
||||
|
||||
(defun tally-orders (orders)
|
||||
(let ((table (make-hash-table)))
|
||||
(dolist (order orders table)
|
||||
(incf (gethash order table 0)))))
|
||||
|
||||
(defun kitchen-count (table item)
|
||||
(multiple-value-bind (count present) (gethash item table)
|
||||
(cond (present count)
|
||||
(t (error 'not-ordered :item item)))))
|
||||
|
||||
(defun price-of (menu item)
|
||||
(let ((pair (assoc item menu)))
|
||||
(if pair (cdr pair) 0)))
|
||||
|
||||
(defun bill-total (orders menu)
|
||||
(let ((total 0))
|
||||
(dolist (order orders total)
|
||||
(incf total (price-of menu order)))))
|
||||
|
||||
(defun order-line (items)
|
||||
(string-downcase (format nil "~{~a~^, ~}" items)))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check (let ((table (tally-orders *orders*)))
|
||||
(and (eql 3 (gethash 'soup table))
|
||||
(eql 2 (gethash 'bread table))
|
||||
(eql 2 (hash-table-count table)))))
|
||||
(check-equal 3 (kitchen-count (tally-orders *orders*) 'soup))
|
||||
(check-error (kitchen-count (tally-orders *orders*) 'pizza) 'not-ordered)
|
||||
(check-equal 7 (price-of *menu* 'soup))
|
||||
(check-equal 0 (price-of *menu* 'pizza))
|
||||
(check-equal 27 (bill-total *orders* *menu*))
|
||||
(check-equal "soup, bread, salad" (order-line '(soup bread salad)))
|
||||
(check-equal 0 (hash-table-count (tally-orders '())))
|
||||
Reference in new issue
Block a user