Skip to content

Instantly share code, notes, and snippets.

@idkjs
Last active December 28, 2021 14:16
Show Gist options
  • Save idkjs/e79a2012129ac761a35deba1606c0957 to your computer and use it in GitHub Desktop.
Save idkjs/e79a2012129ac761a35deba1606c0957 to your computer and use it in GitHub Desktop.
TowersOfHanoi in OCaml

Towers of Hanoi from https://youtu.be/DCKOcY-k2Es

  • add unix.cma to inline using the Unix library when calling the Hanoi.ml program. You could have included it in a dune file.
ocaml unix.cma hanoi.ml
let () =
let instruct i src dsk =
Printf.printf "Moving disk %i from %s to %s\n%!" i src dsk;
Unix.sleep 1
in
let rec hanoi i src dsk aux =
if i == 1 then instruct i src dsk else iterate i src dsk aux
and iterate i src dsk aux =
hanoi (i - 1) src aux dsk;
instruct i src dsk;
hanoi (i - 1) aux dsk src
in
hanoi 3 "A" "B" "C"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment