Files
lisplings/exercises/16_conditions/cond3.lisp
T
venomade 4df60ead93 Lisplings: Rustlings-style exercises for Common Lisp
70 exercises in 20 chapters, from first s-expressions to restarts,
macros, CLOS, and a tiny Lisp interpreter. Each exercise ships
broken with a solved twin and a hint. Includes a watch-mode runner,
a plain-CL check harness, a nix flake, and verification tooling.
All plain SBCL, no dependencies.
2026-11-22 03:30:19 +00:00

27 lines
913 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 '()))