Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Last active July 23, 2022 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/5e5e8bd279535e542f726f9da83f5c10 to your computer and use it in GitHub Desktop.
Save derrickturk/5e5e8bd279535e542f726f9da83f5c10 to your computer and use it in GitHub Desktop.
play a game of "Guess or Die", with aggressive save-scumming
effect QuickSave: int option
effect QuickLoad: int -> empty
let quicksave () = perform QuickSave
let quickload n = absurd (perform (QuickLoad n))
let quicksaving = handler
| effect QuickSave k ->
let rec quickloading () = handler
| effect (QuickLoad n) _ ->
with quickloading () handle
continue k (Some n)
in
with quickloading () handle
continue k None
type outcome =
| Success
| Death
let guess_or_die () =
let r = perform (RandomInt 10) in
fun n -> if n = r then Success else Death;;
let game = guess_or_die ();;
let play game =
let n = match quicksave () with
| None -> 0
| Some n -> n
in
match game n with
| Success -> n
| Death -> quickload (n + 1)
;;
with quicksaving handle
play game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment