emacs.d/clones/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node201.html
2022-08-26 19:11:35 +02:00

111 lines
6.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>22.4. Querying the User</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Querying the User">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html4044 HREF="node202.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4042 HREF="node186.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4038 HREF="node200.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4046 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4047 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4045 HREF="node202.html"> File System Interface</A>
<B>Up:</B> <A NAME=tex2html4043 HREF="node186.html"> Input/Output</A>
<B> Previous:</B> <A NAME=tex2html4039 HREF="node200.html"> Formatted Output to </A>
<HR> <P>
<H1><A NAME=SECTION002640000000000000000>22.4. Querying the User</A></H1>
<P>
<A NAME=25365>The</A>
<A NAME=25366>following</A>
functions provide a convenient and consistent interface for
asking questions of the user. Questions are printed and the answers are
read using the stream <tt>*query-io*</tt>, which normally is synonymous with
<tt>*terminal-io*</tt> but can be rebound to another stream for special
applications.
<P>
<BR><b>[Function]</b><BR>
<tt>y-or-n-p &amp;optional <i>format-string</i> &amp;rest <i>arguments</i></tt><P>This predicate is for asking the user a question whose answer is either
``yes'' or ``no.'' It types out a message (if supplied), reads an answer
in some implementation-dependent manner (intended to be short and simple,
like reading a single character such as <tt>Y</tt> or <tt>N</tt>), and is
true if the answer was ``yes'' or false if the answer was ``no.''
<P>
If the <i>format-string</i> argument is supplied and not <tt>nil</tt>,
then a <tt>fresh-line</tt> operation is performed; then
a message is printed as if the <i>format-string</i> and <i>arguments</i>
were given to <tt>format</tt>.
Otherwise it is assumed that any message has already been printed
by other means.
If you want a question mark at the end of the message,
you must put it there yourself; <tt>y-or-n-p</tt> will not add it.
However, the message should not contain an explanatory note such
as <tt>(Y or N)</tt>, because the nature of the interface provided for
<tt>y-or-n-p</tt> by a given implementation might not involve typing a
character on a keyboard; <tt>y-or-n-p</tt> will provide such a note
if appropriate.
<P>
All input and output are performed using the stream in the global
variable <tt>*query-io*</tt>.
<P>
Here are some examples of the use of <tt>y-or-n-p</tt>:
<P><pre>
(y-or-n-p &quot;Produce listing file?&quot;)
(y-or-n-p &quot;Cannot connect to network host ~S. Retry?&quot; host)
</pre><P>
<P>
<tt>y-or-n-p</tt> should only be used for questions that the user knows
are coming or in situations where the user is known to be waiting for
a response of some kind.
If the user is unlikely to anticipate the question,
or if the consequences of the answer might be grave and irreparable,
then <tt>y-or-n-p</tt> should not be used because the user might type ahead
and thereby accidentally answer the question.
For such questions as ``Shall I delete all of your files?'' it is better to use
<tt>yes-or-no-p</tt>.
<P>
<br><b>[Function]</b><BR>
<tt>yes-or-no-p &amp;optional <i>format-string</i> &amp;rest <i>arguments</i></tt><P>This predicate, like <tt>y-or-n-p</tt>, is for asking the user a question
whose answer is either ``yes'' or ``no.'' It types out a message (if
supplied), attracts the user's attention (for example, by ringing
the terminal's bell), and reads a reply in some
implementation-dependent manner. It is intended that the reply require
the user to take more action than just a single keystroke, such as typing
the full word <tt>yes</tt> or <tt>no</tt> followed by a newline.
<P>
If the <i>format-string</i> argument is supplied and not <tt>nil</tt>,
then a <tt>fresh-line</tt> operation is performed; then
a message is printed as if the <i>format-string</i> and <i>arguments</i>
were given to <tt>format</tt>.
Otherwise it is assumed that any message has already been printed
by other means.
If you want a question mark at the end of the message,
you must put it there yourself; <tt>yes-or-no-p</tt> will not add it.
However, the message should not contain an explanatory note such
as <tt>(Yes or No)</tt> because the nature of the interface provided for
<tt>yes-or-no-p</tt> by a given implementation might not involve typing the
reply on a keyboard; <tt>yes-or-no-p</tt> will provide such a note
if appropriate.
<P>
All input and output are performed using the stream in the global
variable <tt>*query-io*</tt>.
<P>
To allow the user to answer a yes-or-no question with a single
character, use <tt>y-or-n-p</tt>. <tt>yes-or-no-p</tt> should be
used for unanticipated or momentous questions;
this is why it attracts attention
and why it requires a multiple-action sequence to answer it.
<P>
<P>
<BR> <HR><A NAME=tex2html4044 HREF="node202.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4042 HREF="node186.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4038 HREF="node200.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4046 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4047 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4045 HREF="node202.html"> File System Interface</A>
<B>Up:</B> <A NAME=tex2html4043 HREF="node186.html"> Input/Output</A>
<B> Previous:</B> <A NAME=tex2html4039 HREF="node200.html"> Formatted Output to </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>