Common Lisp the Language, 2nd Edition


next up previous contents index
Next: File System Interface Up: Input/Output Previous: Formatted Output to

22.4. Querying the User

The following functions provide a convenient and consistent interface for asking questions of the user. Questions are printed and the answers are read using the stream *query-io*, which normally is synonymous with *terminal-io* but can be rebound to another stream for special applications.


[Function]
y-or-n-p &optional format-string &rest arguments

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 Y or N), and is true if the answer was ``yes'' or false if the answer was ``no.''

If the format-string argument is supplied and not nil, then a fresh-line operation is performed; then a message is printed as if the format-string and arguments were given to format. 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; y-or-n-p will not add it. However, the message should not contain an explanatory note such as (Y or N), because the nature of the interface provided for y-or-n-p by a given implementation might not involve typing a character on a keyboard; y-or-n-p will provide such a note if appropriate.

All input and output are performed using the stream in the global variable *query-io*.

Here are some examples of the use of y-or-n-p:

(y-or-n-p "Produce listing file?") 
(y-or-n-p "Cannot connect to network host ~S.  Retry?" host)

y-or-n-p 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 y-or-n-p 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 yes-or-no-p.


[Function]
yes-or-no-p &optional format-string &rest arguments

This predicate, like y-or-n-p, 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 yes or no followed by a newline.

If the format-string argument is supplied and not nil, then a fresh-line operation is performed; then a message is printed as if the format-string and arguments were given to format. 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; yes-or-no-p will not add it. However, the message should not contain an explanatory note such as (Yes or No) because the nature of the interface provided for yes-or-no-p by a given implementation might not involve typing the reply on a keyboard; yes-or-no-p will provide such a note if appropriate.

All input and output are performed using the stream in the global variable *query-io*.

To allow the user to answer a yes-or-no question with a single character, use y-or-n-p. yes-or-no-p 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.



next up previous contents index
Next: File System Interface Up: Input/Output Previous: Formatted Output to


AI.Repository@cs.cmu.edu