Last active
July 7, 2021 21:43
-
-
Save juan-reynoso/c655509675ee133d6ae228736f7896e0 to your computer and use it in GitHub Desktop.
Restarts, interactive choices in the debugger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-condition odd-number-error (error) | |
((message :initarg :message :reader message)) | |
(:report (lambda (condition stream) | |
(format stream "Ups! we have an error. ~%~a" (message condition)))) | |
(:documentation "Custom error when we encounter a odd number error.")) | |
(defun filter-oddp (sequence) | |
"Iterates over the elememets of the sequence and display | |
the odd number, and otherwise invoke a condition." | |
(dolist (x sequence) | |
(if (oddp x) | |
(format t "Odd number: ~a ~%" x) | |
(error 'odd-number-error :message (format nil "~a is not an odd number." x))))) | |
(defun filter-odd-numbers (sequence) | |
"Display the number if is odd, and otherwise invoke restarts." | |
;; Restarts, interactive choices in the debugger | |
(restart-case (filter-oddp sequence) | |
;; Creates a restart called "SKIP-FILTER-ODDP" | |
(skip-filter-oddp () | |
nil) | |
;; Creates a restart called "RETURN-SEQUENCE" | |
(return-sequence () | |
sequence) | |
;; Creates a restart called "SAY-GOOD-BYE" | |
(say-good-by () | |
"Good bye."))) | |
;; call the function | |
(filter-odd-numbers (list 1 3 5 8 9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment