I was reflecting on how the Towers of Hanoi problem was important in
my development as a programmer --- my Microsoft-Basic-limited idea of
"programming" was dramatically expanded when I saw a recursive
towers-of-Hanoi program in Pascal that was less than a screenful, and
of course really understanding recursion is a real mind-blower.

But in a modern programming language, Hanoi can be even shorter:

    (* move n discs from a to z (using third peg named x) *)
    let rec hanoi move n a z x = if n = 0 then () else 
      (hanoi move (n-1) a x z ; move n a z ; hanoi move (n-1) x z a)
    let printmove disc a b = print_endline 
        ("Move disc " ^ (string_of_int disc) ^ " from " ^ a ^ " to " ^ b) ;;
    hanoi printmove 4 "peg A" "peg C" "peg B"

Reply via email to