Understanding evaluation of I/O in conditional

Running the following code:

let input_bool () = input_char stdin = 't'

let () =
  if
    let x = input_bool () in
    let y = input_bool () in
    x || y
  then
    print_endline "yes"

At runtime I am prompted only for a single char and, when entering t, the output yes is immediately printed. Can anyone explain why the second call to input_bool () is not happening?

EDIT: the same behaviour on changing the || to &&, so I assume it’s not explained by the compiler doing some fancy inlining.

Your keyboard/terminal by default operates in canonical mode, so unless you first enter a newline, you will always have at least two characters read consecutively. First the letter entered, and then, if followed by a newline via the Enter key, the ‘\n’ character.

3 Likes

Thanks! Simple in retrospect, but I guess I had a brain freeze :slight_smile: