kernelthread.com

Objective Caml

The Towers of Hanoi as an Objective Caml program.

According to the Objective Caml web site:

Objective Caml is the latest implementation of the Caml dialect of ML. The main novelties compared with its ancestor, Caml Light, are:

(* The Towers Of Hanoi *) (* Objective Caml *) (* Copyright (C) 2002 Amit Singh. All Rights Reserved. *) (* http://hanoi.kernelthread.com *) (* *) (* Last tested under Objective Caml Version 3.06 *) (* http://caml.inria.fr *) (* *) let movedisk f t = print_endline(f ^ " --> " ^ t);; let rec dohanoi n f u t = if n = 1 then movedisk f t else begin dohanoi (n - 1) f t u; movedisk f t; dohanoi (n - 1) u f t; end;; let hanoi n = if n > 0 then dohanoi n "1" "2" "3";; if !Sys.interactive then () else begin let argc = Array.length Sys.argv in if argc != 2 then begin prerr_endline("usage: hanoi n"); exit 1 end; hanoi (int_of_string (Sys.argv.(1))); exit 0 end;;

Download

hanoi.ocaml