The less-familiar parts of Lisp for beginners — restart-bind

Continuing through the list of somewhat obscure Lisp features, we come to restart-bind.  This is a fairly low-level tool for configuring restarts.  A review of Lisp restarts is available in this earlier article.  While the restart system can appear complicated, it is generally useful to think of restarts as dynamically-scoped functions that can be invoked under certain conditions.  The restart-bind macro is the low-level glue to handle this, and it’s unusual that the full power of this macro is needed.  If you can achieve your intended results with restart-case, it is recommended that you use that, instead.

The restart-bind macro has three optional key/value pair arguments that affect behaviour in interactive contexts.

A simple example of the basic syntax follows.  Note that this example could also be easily coded with restart-case:
*slime-repl sbcl*

CL-USER> (restart-bind
             ((say-hi #'(lambda ()
                          (format t "Hello~%"))))
           (dotimes (i 10)
             (format t "i= ~D~%" i)
             (when (= (mod i 4) 3)
               (invoke-restart 'say-hi))))
i= 0
i= 1
i= 2
i= 3
Hello
i= 4
i= 5
i= 6
i= 7
Hello
i= 8
i= 9
NIL

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.