Files
lisplings/exercises/16_conditions/cond3.lisp
T

28 lines
950 B
Common Lisp

;;; CONDITIONS 3: YOUR OWN CONDITION TYPES
;;;
;;; Conditions are classes. DEFINE-CONDITION defines new ones with
;;; :INITARGs; ERROR signals them; HANDLER-CASE catches by type.
(define-condition empty-plate (error)
((what :initarg :what :reader empty-plate-what))
(:documentation "Signaled when a plate turns out to be empty."))
;; TODO: eat returns "yum" - unless XS is NIL, when it signals
;;; empty-plate with :what "soup".
(defun eat (xs)
"yum")
;; TODO: catch-empty calls (eat xs); when the plate is empty it
;;; returns the condition's WHAT instead.
(defun catch-empty (xs)
(eat xs))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "yum" (catch-empty '(a b)))
(check-equal "soup" (catch-empty '()))
(check-error (eat '()) 'empty-plate)