

Task

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.




import System.IO
 
hFlushInput :: Handle -> IO ()
hFlushInput hdl = do
  r <- hReady hdl
  if r then do
    c <- hGetChar hdl
    hFlushInput hdl
  else
    return ()
 
yorn :: IO Char
yorn = do
  c <- getChar
  if c == 'Y' || c == 'N' then return c
  else if c == 'y' then return 'Y'
  else if c == 'n' then return 'N'
  else yorn
 
main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  putStr "Press Y or N to continue: "
 
  hSetBuffering stdin NoBuffering
  hSetEcho stdin False
  hFlushInput stdin
  answer <- yorn
  putStrLn [answer]