Skip to main content

y-or-n-p, yes-or-no-p

y-or-n-p, yes-or-no-p Function

Syntax:

y-or-n-p &optional control &rest arguments → generalized-boolean

yes-or-no-p &optional control &rest arguments → generalized-boolean

Arguments and Values:

control—a format control.

argumentsformat arguments for control.

generalized-boolean—a generalized boolean.

Description:

These functions ask a question and parse a response from the user. They return true if the answer is affirmative, or false if the answer is negative.

y-or-n-p is for asking the user a question whose answer is either “yes” or “no.” It is intended that the reply require the user to answer a yes-or-no question with a single character. yes-or-no-p is also for asking the user a question whose answer is either “Yes” or “No.” 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.

y-or-n-p types out a message (if supplied), reads an answer in some implementation-dependent manner (intended to be short and simple, such as reading a single character such as Y or N). yes-or-no-p types out a message (if supplied), attracts the user’s attention (for example, by ringing the terminal’s bell), and reads an answer in some implementation-dependent manner (intended to be multiple characters, such as YES or NO).

If format-control is supplied and not nil, then a fresh-line operation is performed; then a message is printed as if format-control and arguments were given to format. In any case, yes-or-no-p and y-or-n-p will provide a prompt such as “(Y or N)” or “(Yes or No)” if appropriate.

All input and output are performed using query I/O.

Examples:

(y-or-n-p "(t or nil) given by") 
(t or nil) given by (Y or N) Y
→ true
(yes-or-no-p "a ~S message" ’frightening)
▷ a FRIGHTENING message (Yes or No) no
→ false
(y-or-n-p "Produce listing file?")
▷ Produce listing file?
▷ Please respond with Y or N. n
→ false

Side Effects:

Output to and input from query I/O will occur.

Affected By:

*query-io*.

See Also:

format

Notes:

yes-or-no-p and yes-or-no-p do not add question marks to the end of the prompt string, so any desired question mark or other punctuation should be explicitly included in the text query.

Expanded Reference: y-or-n-p, yes-or-no-p

Overview

y-or-n-p and yes-or-no-p are interactive functions that ask the user a yes/no question via *query-io* and return a generalized boolean. They require user interaction and cannot be demonstrated in non-interactive examples.

y-or-n-p with a Message

y-or-n-p expects a quick single-character response (e.g., Y or N).

;; Interactive usage (requires user input):
;; (y-or-n-p "Save changes?")
;; prints: Save changes? (Y or N)
;; If user types Y: returns T
;; If user types N: returns NIL

yes-or-no-p with a Message

yes-or-no-p expects the user to type the full word "yes" or "no".

;; Interactive usage:
;; (yes-or-no-p "Delete all files?")
;; prints: Delete all files? (Yes or No)
;; If user types yes: returns T
;; If user types no: returns NIL

Format Control Arguments

Both functions accept format control strings and arguments, just like format.

;; (y-or-n-p "Process ~D items?" 42)
;; prints: Process 42 items? (Y or N)

;; (yes-or-no-p "Overwrite ~A?" "data.txt")
;; prints: Overwrite data.txt? (Yes or No)

Without a Message

When called with no arguments, only the prompt (e.g., "(Y or N)") is displayed.

;; (y-or-n-p)
;; prints: (Y or N)