Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Last active July 7, 2021 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-reynoso/c655509675ee133d6ae228736f7896e0 to your computer and use it in GitHub Desktop.
Save juan-reynoso/c655509675ee133d6ae228736f7896e0 to your computer and use it in GitHub Desktop.
Restarts, interactive choices in the debugger
(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