The following code works without errors:
let fxy = print_int (max xy); print_char ' '; print_int (x + y) in for i = 1 to Scanf.scanf "%d" (fun x -> x) do Scanf.scanf "\n%d %d" f; print_newline (); done;
But when I declare the fmt variable to store the format "\ n% d% d" and pass it to scanf, I get an error message, here is the new code:
let fxy = print_int (max xy); print_char ' '; print_int (x + y) in let fmt = "\n%d %d" in (* added *) for i = 1 to Scanf.scanf "%d" (fun x -> x) do Scanf.scanf fmt f; (* edited *) print_newline (); done;
I get this error:
File "prog.ml", line 7, characters 16-19: Error: This expression has type string but an expression was expected of type ('a, Scanf.Scanning.in_channel, 'b, 'c -> 'd, 'a -> 'e, 'e) format6
Why does it work differently? Is there a difference between the two codes?
source share