Last active
January 2, 2016 01:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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