

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.




open System
 
let rec yorn () =
    let rec flush () = if Console.KeyAvailable then ignore (Console.ReadKey()); flush ()
    flush ()
 
    printf "\nY or N? "
    match Console.ReadKey().Key with
    | ConsoleKey.Y -> 'Y'
    | ConsoleKey.N -> 'N'
    | _ -> yorn()
 
printfn "\nYour choice: %c" (yorn())