23 lines
809 B
Common Lisp
23 lines
809 B
Common Lisp
;;; 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"))
|