以下のコードは、事前定義された関数 moveLOD、swapLOI、および swapLID を使用して手のリストを返す hanoi を解決します。
MoveLOD: 1 つのディスクを 1 番目の位置から 3 番目のピンの 3 番目の位置に移動します。さらに、動きに関する情報を含む文字列が文字列のリストに積み上げられています。
type Pin = (Char, Int) -- Represents a rod, named for a character and the number of disks in it.
type Plate = (Pin, Pin, Pin) -- Represents the configuration of the three rods.(Origin,Intermediate,Destination
type Log = (Plate, [String]) -- Represents a state formed by the configuration of rods and a list of strings that will record the movements made by the algorithm.
moveLOD :: Log -> Log
moveLOD (((o,n), i ,(d,k)),s) = (((o,n-1), i ,(d,k+1)), (o:" -> " ++ [d]):s)
-- swapLOI: Change the positions of the origin rods and intermediate rods.
swapLOI:: Log->Log
swapLOI ((o,i,d),s) = ((i,o,d),s)
-- swapoLID : Change the positions of the intermediate rods and destination rods.
swapLID:: Log->Log
swapLID ((o,i,d),s) = ((o,d,i),s)
hanoi :: Log -> Log
hanoi:: Int->Log->[String]
hanoi 1 log = transformaLista(moveLOD log)
hanoi n log = hanoi (n-1) (swapLID log) ++ hanoi 1 log ++ hanoi (n-1) (swapLOI(log))
changeToList::Log->[String]
changeToList(p,s) = s
callHanoi:: Int->[String]
callHanoi n = hanoi n ((('O',n),('I',0),('D',0)),[])