Files
lisplings/solutions/04_conditionals/if5.lisp
T

27 lines
842 B
Common Lisp

;;; CONDITIONALS 5: CASE AND ECASE (solution)
(defun rate-fruit (fruit)
(case fruit
((apple banana) "cheap")
((mango durian) "pricey")
(t "unknown")))
(defun door-state (state)
(ecase state
(:open "come in")
(:closed "knock first")))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal "cheap" (rate-fruit 'apple))
(check-equal "cheap" (rate-fruit 'banana))
(check-equal "pricey" (rate-fruit 'mango))
(check-equal "pricey" (rate-fruit 'durian))
(check-equal "unknown" (rate-fruit 'kiwi))
(check-equal "come in" (door-state :open))
(check-equal "knock first" (door-state :closed))
(check-error (door-state :ajar) 'type-error)