116 lines
7.1 KiB
HTML
116 lines
7.1 KiB
HTML
|
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
||
|
<HTML>
|
||
|
<HEAD>
|
||
|
<TITLE>CLHS: Macro WITH-SIMPLE-RESTART</TITLE>
|
||
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
||
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
||
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
||
|
<LINK REL=TOP HREF="../Front/index.htm">
|
||
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
||
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
||
|
<LINK REL=PREV HREF="m_w_cnd_.htm">
|
||
|
<LINK REL=UP HREF="c_condit.htm">
|
||
|
<LINK REL=NEXT HREF="r_abort.htm">
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="m_w_cnd_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_condit.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="r_abort.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
||
|
|
||
|
<HR>
|
||
|
|
||
|
<A NAME="with-simple-restart"><I>Macro</I> <B>WITH-SIMPLE-RESTART</B></A> <P>
|
||
|
<P><B>Syntax:</B><P>
|
||
|
<P>
|
||
|
|
||
|
<B>with-simple-restart</B> <I>(name format-control <I>format-argument</I><B>*</B>) <I>form</I><B>*</B></I><P> => <I><I>result</I><B>*</B></I><P>
|
||
|
<P>
|
||
|
<P><B>Arguments and Values:</B><P>
|
||
|
<P>
|
||
|
<I>name</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>. <P>
|
||
|
<I>format-control</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#format_control"><I>format control</I></A>. <P>
|
||
|
<I>format-argument</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> (i.e., a <A REL=DEFINITION HREF="26_glo_f.htm#format_argument"><I>format argument</I></A>). <P>
|
||
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
||
|
<I>results</I>---in the normal situation, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I>; in the exceptional situation where the <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> named <I>name</I> is invoked, two values---<A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> and <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>. <P>
|
||
|
<P><B>Description:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="#with-simple-restart"><B>with-simple-restart</B></A> establishes a restart. <P>
|
||
|
If the restart designated by <I>name</I> is not invoked while executing <I>forms</I>, all values returned by the last of <I>forms</I> are returned. If the restart designated by <I>name</I> is invoked, control is transferred to <A REL=DEFINITION HREF="#with-simple-restart"><B>with-simple-restart</B></A>, which returns two values, <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> and <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>. <P>
|
||
|
If <I>name</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, an anonymous restart is established. <P>
|
||
|
The <I>format-control</I> and <I>format-arguments</I> are used report the <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A>. <P>
|
||
|
<P><B>Examples:</B><P>
|
||
|
<P>
|
||
|
<PRE>
|
||
|
(defun read-eval-print-loop (level)
|
||
|
(with-simple-restart (abort "Exit command level ~D." level)
|
||
|
(loop
|
||
|
(with-simple-restart (abort "Return to command level ~D." level)
|
||
|
(let ((form (prog2 (fresh-line) (read) (fresh-line))))
|
||
|
(prin1 (eval form)))))))
|
||
|
=> READ-EVAL-PRINT-LOOP
|
||
|
(read-eval-print-loop 1)
|
||
|
(+ 'a 3)
|
||
|
>> Error: The argument, A, to the function + was of the wrong type.
|
||
|
>> The function expected a number.
|
||
|
>> To continue, type :CONTINUE followed by an option number:
|
||
|
>> 1: Specify a value to use this time.
|
||
|
>> 2: Return to command level 1.
|
||
|
>> 3: Exit command level 1.
|
||
|
>> 4: Return to Lisp Toplevel.
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
<PRE>
|
||
|
(defun compute-fixnum-power-of-2 (x)
|
||
|
(with-simple-restart (nil "Give up on computing 2^~D." x)
|
||
|
(let ((result 1))
|
||
|
(dotimes (i x result)
|
||
|
(setq result (* 2 result))
|
||
|
(unless (fixnump result)
|
||
|
(error "Power of 2 is too large."))))))
|
||
|
COMPUTE-FIXNUM-POWER-OF-2
|
||
|
(defun compute-power-of-2 (x)
|
||
|
(or (compute-fixnum-power-of-2 x) 'something big))
|
||
|
COMPUTE-POWER-OF-2
|
||
|
(compute-power-of-2 10)
|
||
|
1024
|
||
|
(compute-power-of-2 10000)
|
||
|
>> Error: Power of 2 is too large.
|
||
|
>> To continue, type :CONTINUE followed by an option number.
|
||
|
>> 1: Give up on computing 2^10000.
|
||
|
>> 2: Return to Lisp Toplevel
|
||
|
>> Debug> :continue 1
|
||
|
=> SOMETHING-BIG
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
<P><B>Side Effects:</B> None.
|
||
|
<P>
|
||
|
<P><B>Affected By:</B> None.
|
||
|
<P>
|
||
|
<P><B>Exceptional Situations:</B> None.
|
||
|
<P>
|
||
|
<P><B>See Also:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="m_rst_ca.htm#restart-case"><B>restart-case</B></A> <P>
|
||
|
<P><B>Notes:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="#with-simple-restart"><B>with-simple-restart</B></A> is shorthand for one of the most common uses of <A REL=DEFINITION HREF="m_rst_ca.htm#restart-case"><B>restart-case</B></A>. <P>
|
||
|
<A REL=DEFINITION HREF="#with-simple-restart"><B>with-simple-restart</B></A> could be defined by: <P>
|
||
|
|
||
|
<PRE>
|
||
|
(defmacro with-simple-restart ((restart-name format-control
|
||
|
&rest format-arguments)
|
||
|
&body forms)
|
||
|
`(restart-case (progn ,@forms)
|
||
|
(,restart-name ()
|
||
|
:report (lambda (stream)
|
||
|
(format stream ,format-control ,@format-arguments))
|
||
|
(values nil t))))
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
Because the second return value is <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A> in the exceptional case, it is common (but not required) to arrange for the second return value in the normal case to be missing or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> so that the two situations can be distinguished. <P>
|
||
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss170.htm">FORMAT-STRING-ARGUMENTS:SPECIFY</A><P></UL><HR>
|
||
|
|
||
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
||
|
|
||
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
||
|
</BODY>
|
||
|
</HTML>
|