63 lines
2.1 KiB
Common Lisp
63 lines
2.1 KiB
Common Lisp
;;; QUIZ 3: THE LUNCH TALLY
|
|
;;;
|
|
;;; Everything from the chapters before macros in one working
|
|
;;; program: the waiters' pad is a list of orders, the kitchen
|
|
;;; counts them in a hash table, the prices live in an association
|
|
;;; list, and FORMAT writes the tally back out as a string.
|
|
;;;
|
|
;;; A price pair is (ITEM . COST), as in 06_lists: ASSOC finds the
|
|
;;; pair, its CDR is the cost.
|
|
|
|
(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)))
|
|
|
|
;; TODO: A fresh hash table mapping each ordered item to the number
|
|
;; of times it appears on the pad.
|
|
(defun tally-orders (orders)
|
|
(let ((table (make-hash-table)))
|
|
table))
|
|
|
|
;; TODO: How many of ITEM the kitchen must cook. Nobody ordered it
|
|
;;; at all? Signal not-ordered with :item ITEM.
|
|
(defun kitchen-count (table item)
|
|
0)
|
|
|
|
;; TODO: The price of ITEM from the menu alist, or 0 when the
|
|
;;; kitchen does not serve it.
|
|
(defun price-of (menu item)
|
|
0)
|
|
|
|
;; TODO: What the table owes: the price of every ordered item,
|
|
;;; added up.
|
|
(defun bill-total (orders menu)
|
|
0)
|
|
|
|
;; TODO: The items as one lowercase comma-separated line:
|
|
;;; "soup, bread, salad" for (SOUP BREAD SALAD).
|
|
(defun order-line (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 '())))
|