Skip to content

Instantly share code, notes, and snippets.

@kfl
Last active March 14, 2019 12:41
Show Gist options
  • Save kfl/5643080 to your computer and use it in GitHub Desktop.
Save kfl/5643080 to your computer and use it in GitHub Desktop.
Fun with side-effects in SML
(* 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