For me the most tricky part in Ocaml is reading the values from stdin so that I dont get NZEC or WA.
At the moment the only safe way I know is a combination of read_line() and Scanf.sscanf.

For example for FENCE1 I read the input like this. The value is stored in u after reading.

let su = read_line() in
let u = Scanf.sscanf su "%d" (fun i -> i) in

For NSTEPS I read the input like this. The values x and y are forwarded to the function nsteps after reading.

let sr = read_line() in
Scanf.sscanf sr "%d %d" nsteps;

Are there any smarter or shorter ways of doing this?

  • created

    Aug '09
  • last reply

    Nov '10
  • 1

    reply

  • 1.2k

    views

  • 2

    users

1 year later

You can do it this way (for ints, floats, and strings respectively):

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x) 
let read_float () = Scanf.bscanf Scanf.Scanning.stdib " %f " (fun x -> x) 
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

--D. Sleator