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,40 @@
|
||||
;;; CONDITIONS 5: REPORTS, WARN, SIGNAL (solution)
|
||||
|
||||
(define-condition low-battery (condition) ())
|
||||
|
||||
(define-condition empty-cart (error)
|
||||
()
|
||||
(:report "The cart is empty - nothing to check out."))
|
||||
|
||||
(defun checkout (items)
|
||||
(when (null items)
|
||||
(error 'empty-cart))
|
||||
(list 'order items))
|
||||
|
||||
(defun battery-check (percent)
|
||||
(when (< percent 20)
|
||||
(warn "battery ~a%!" percent))
|
||||
(list 'battery percent))
|
||||
|
||||
(defun check-battery (percent)
|
||||
(when (<= percent 10)
|
||||
(signal '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,22 @@
|
||||
;;; CONDITIONS 6: RESTARTS WITH ARGUMENTS, IGNORE-ERRORS (solution)
|
||||
|
||||
(defun parse-count (text)
|
||||
(restart-case (parse-integer text)
|
||||
(use-value (value)
|
||||
value)))
|
||||
|
||||
(defun maybe-age (text)
|
||||
(ignore-errors (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