Towers of Hanoi
Towers of Hanoi is a puzzle game consisting of disks of distinct sizes placed on one of three pegs in order of decreasing size (from top to bottom). The goal is to move a stack of disks on one peg (let's say the left) to another (let's say the right), with the following restrictions:
- only one disk may be moved at a time;
- each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty peg;
- no disk may be placed on top of a disk that is smaller than it.
See the Towers of Hanoi Wikipedia page for more information about the rules.
Implement the function
val hanoi : int -> unit
so that hanoi n prints out the moves required to solve the towers
of Hanoi problem with n disks, moving the stack from the left peg
to the right peg (using the middle peg as an auxiliary), one move
per line, in the following format.
"move disk from <src> peg to <dst> peg"
Examples
hanoi 3 prints:
move disk from left peg to right peg move disk from left peg to middle peg move disk from right peg to middle peg move disk from left peg to right peg move disk from middle peg to left peg move disk from middle peg to right peg move disk from left peg to right peg