soln = title++hanoi 8 "A" "B" "C" title = "SOLUTION TO TOWERS OF HANOI WITH 8 DISCS\n\n" hanoi 0 a b c = [] hanoi (n+1) a b c = hanoi n a c b ++ move a b ++ hanoi n c b a move a b = "move the top disc from "++a++" to "++b++"\n"
This script generates a solution to the well known `Towers of Hanoi' problem. There are three pegs, A, B, C, say. On the first peg is a conical pile of discs, each disc smaller than the one below. The goal is to move all the discs to B, using C as a spare, moving one disc at time without ever putting a disc on top of a smaller one. To see the moves for a game with 8 discs say
solnthe output looks like this:
SOLUTION TO TOWERS OF HANOI WITH 8 DISCS move the top disc from A to C move the top disc from A to B move the top disc from C to B move the top disc from A to C move the top disc from B to A move the top disc from B to C move the top disc from A to C move the top disc from A to B move the top disc from C to B move the top disc from C to A move the top disc from B to A move the top disc from C to B move the top disc from A to C move the top disc from A to B move the top disc from C to B move the top disc from A to C move the top disc from B to A ..... (255 moves in total)According to legend, the game is being played with 64 gold discs in a Bhuddist monastery. When the monks have finished the game, the world will cease to exist ...