Last active
March 14, 2019 12:41
-
-
Save kfl/5643080 to your computer and use it in GitHub Desktop.
Fun with side-effects in SML
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
(* Moscow ML handle the first three tests correct. *) | |
fun csnd x y = y | |
local val r = ref "WRONG" in | |
val test1 = csnd (r := "OK"; ()) (!r); | |
end | |
local | |
val r = ref "WRONG" | |
val fr = ref (fn () => (r := "OK"; csnd)) | |
in | |
val test2 = (!fr ()) () !r; | |
end | |
exception Right and Wrong | |
val test3 = csnd (raise Right) (raise Wrong) | |
handle Right => "OK" | |
| Wrong => "WRONG" | |
(* But it's downhill from here... *) | |
local | |
fun f y = (raise Right; fn x => x) | |
in | |
val test4 = (f 7 (raise Wrong)) | |
handle Right => "OK" | |
| Wrong => "WRONG" | |
end | |
local | |
val r = ref "WRONG" | |
fun f y = #2 (r := "OK", fn x => x) | |
in | |
val test5 = (f 7 (!r), !r) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment