Plaster

common-lisp
(defmacro retrying (n &body body) (alexandria:with-gensyms (i out loop) (alexandria:once-only (n) `(loop :named ,out :for ,i :from 0 :below ,n :do (block ,loop (format t "Try ~a~%" ,i) (handler-bind ((error (lambda (c) (declare (ignore c)) (return-from ,loop)))) (return-from ,out (values (progn ,@body) t)))) :finally (return-from ,out (values nil nil)))))) (multiple-value-bind (res suc) (retrying (+ 1 2) (when (zerop (random 2)) (format t "Signaling an error!~%") (error "Error")) 123) (if suc (format t "Succeeded with: ~a~%" res) (format t "Exceeded!~%")))