How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Obtain a valid   Y   or   N   response from the keyboard. The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated. The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Common Lisp programming language

Source code in the common programming language

(defun rosetta-y-or-n ()
  (clear-input *query-io*)
  (y-or-n-p))


(defun y-or-n ()
  (clear-input *standard-input*)
  (loop as dum = (format t "Y or N for yes or no: ")
        as c = (read-char)
        as q = (and (not (equal c #\n)) (not (equal c #\y)))
        when q do (format t "~%Need Y or N~%")
        unless q return (if (equal c #\y) 'yes 'no)))


(defun y-or-no ()
  (with-screen (scr :input-buffering nil :input-blocking t)
    (clear scr)
    (princ "Do you want to continue? [Y/N]" scr)
    (refresh scr)
    (event-case (scr event)
      ((#\Y #\y) (return-from event-case t))
      ((#\N #\n) (return-from event-case nil)))))


  

You may also check:How to resolve the algorithm User input/Graphical step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the BQN programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the Python programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the R programming language