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,53 @@
|
||||
;;; CONDITIONS 5: REPORTS, WARN, SIGNAL
|
||||
;;;
|
||||
;;; DEFINE-CONDITION (exercise 3) names a condition class; the
|
||||
;;; :REPORT option gives it the text humans see - and PRINC of the
|
||||
;;; condition prints that text.
|
||||
;;;
|
||||
;;; ERROR unwinds to the debugger. WARN reports and CARRIES ON - it
|
||||
;;; signals a WARNING, catchable like anything else. SIGNAL is the
|
||||
;;; bare mechanism: handlers see it, nobody else is bothered.
|
||||
|
||||
(define-condition low-battery (condition) ())
|
||||
|
||||
;; TODO: A condition of your own, EMPTY-CART - a subclass of ERROR
|
||||
;; with :report "The cart is empty - nothing to check out." - and
|
||||
;; CHECKOUT signals it when ITEMS is NIL.
|
||||
(define-condition empty-cart (error)
|
||||
())
|
||||
|
||||
(defun checkout (items)
|
||||
(when (null items)
|
||||
(error 'empty-cart))
|
||||
(list 'order items))
|
||||
|
||||
;; TODO: BATTERY-CHECK warns "battery ~a%!" when PERCENT is under
|
||||
;; 20 - and returns (list 'battery percent) either way.
|
||||
(defun battery-check (percent)
|
||||
(error "battery ~a%!" percent)
|
||||
(list 'battery percent))
|
||||
|
||||
;; TODO: CHECK-BATTERY signals LOW-BATTERY when PERCENT is at or
|
||||
;; under 10 - a plain condition, not an error - then returns
|
||||
;; (list 'battery percent).
|
||||
(defun check-battery (percent)
|
||||
(error "low battery")
|
||||
(list 'battery percent))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal '(order (milk)) (checkout '(milk)))
|
||||
(check-error (checkout '()) 'empty-cart)
|
||||
(check-equal "The cart is empty - nothing to check out."
|
||||
(handler-case (checkout '())
|
||||
(empty-cart (c) (format nil "~a" c))))
|
||||
(check-equal '(battery 15) (battery-check 15))
|
||||
(check-equal '(battery 90) (battery-check 90))
|
||||
(check-equal :noticed (handler-case (warn "battery low")
|
||||
(warning () :noticed)))
|
||||
(check-equal :swapped (handler-case (check-battery 5)
|
||||
(low-battery () :swapped)))
|
||||
(check-equal '(battery 20) (check-battery 20))
|
||||
@@ -0,0 +1,36 @@
|
||||
;;; CONDITIONS 6: RESTARTS WITH ARGUMENTS, IGNORE-ERRORS
|
||||
;;;
|
||||
;;; Exercise 4's restart carried no luggage. USE-VALUE takes an
|
||||
;;; argument - a replacement the handling code chooses - and
|
||||
;;; STORE-VALUE is its cousin for storing one and trying again.
|
||||
;;; INVOKE-RESTART passes it over:
|
||||
;;;
|
||||
;;; (handler-bind ((parse-error
|
||||
;;; (lambda (c) (invoke-restart 'use-value 42))))
|
||||
;;; (parse-count "abc"))
|
||||
;;;
|
||||
;;; IGNORE-ERRORS wraps a form in cotton wool: anything it signals
|
||||
;;; turns into NIL, and the condition rides along as a second
|
||||
;;; value.
|
||||
|
||||
;; TODO: Parse TEXT as an integer - but offer a USE-VALUE restart
|
||||
;; that answers with whatever value the caller hands over.
|
||||
(defun parse-count (text)
|
||||
(parse-integer text))
|
||||
|
||||
;; TODO: The integer in TEXT, or NIL when TEXT does not parse.
|
||||
(defun maybe-age (text)
|
||||
(parse-integer text))
|
||||
|
||||
;;; ------------------------------------------------------------------
|
||||
;;; Checks. Read them - they tell you what to aim for - but leave
|
||||
;;; them unchanged.
|
||||
;;; ------------------------------------------------------------------
|
||||
|
||||
(check-equal 42 (handler-bind ((parse-error (lambda (c)
|
||||
(declare (ignore c))
|
||||
(invoke-restart 'use-value 42))))
|
||||
(parse-count "abc")))
|
||||
(check-equal 7 (parse-count "7"))
|
||||
(check-equal 42 (maybe-age "42"))
|
||||
(check-equal nil (maybe-age "abc"))
|
||||
Reference in new issue
Block a user