Skip to content

Instantly share code, notes, and snippets.

@swistak35
Last active January 2, 2016 01:19
Show Gist options
  • Save swistak35/8229326 to your computer and use it in GitHub Desktop.
Save swistak35/8229326 to your computer and use it in GitHub Desktop.
Strange behavior of IO in Ocaml. It firstly reads value for scanf, and after that printf all stuff (even those, which it should print before scanf). test_rw2 contains even simpler example.
type ast = Cons of ast * ast
| Write
| Read
open Printf
open Scanf
let myVar = ref 0
let set_myVar x =
myVar := x
let rec interpret ast = match ast with
| Cons(s1, s2) ->
interpret s1;
interpret s2;
| Write -> printf "myVar = %d\n" !myVar
| Read ->
begin
printf "? myVar = ";
scanf "%d" (set_myVar);
end
let _ = interpret (Cons(Write,Cons(Read,Write)))
open Printf
open Scanf
let myVar = ref 0
let set_myVar x =
myVar := x
let _ =
begin
printf "myVar = %d\n" !myVar;
printf "? myVar = ";
scanf "%d" (set_myVar);
printf "myVar = %d\n" !myVar;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment