Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a character in OCaml without a return key?

Tags:

ocaml

I'm looking for something like input_char stdin but without waiting for a return key. I would not to depend on a big dependency like lambda-term.

like image 245
rgrinberg Avatar asked Nov 16 '12 03:11

rgrinberg


People also ask

What does :: do in OCaml?

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).

What does |> mean in OCaml?

The |> operator represents reverse function application.

How do I return something to OCaml?

OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.

What does Type A mean in OCaml?

The type 'a is a type variable, and stands for any given type. The reason why sort can apply to lists of any type is that the comparisons (=, <=, etc.) are polymorphic in OCaml: they operate between any two values of the same type. This makes sort itself polymorphic over all list types.


1 Answers

Handling input in full lines is easy. Handling it a character at a time is a little bit system dependent. If you're on a Unix-like system you should be able to do this using the Unix module:

let get1char () =
    let termio = Unix.tcgetattr Unix.stdin in
    let () =
        Unix.tcsetattr Unix.stdin Unix.TCSADRAIN
            { termio with Unix.c_icanon = false } in
    let res = input_char stdin in
    Unix.tcsetattr Unix.stdin Unix.TCSADRAIN termio;
    res
like image 121
Jeffrey Scofield Avatar answered Oct 27 '22 00:10

Jeffrey Scofield