haskell-homework/lib/Homework/Ch01/TowerOfHanoi.hs

17 lines
511 B
Haskell

module Homework.Ch01.TowerOfHanoi where
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi n x y z = runHanoi [] (reverse $ initStack n) [] []
where
initStack 0 = []
initStack xx = xx : initStack (xx - 1)
runHanoi moves (itemX : stackX) [] [] = runHanoi ((x, z) : moves) stackX [] [itemX]
runHanoi moves (itemX : stackX) [] (itemZ : stackZ)
| itemX > itemZ = runHanoi ((x, y) : moves) stackX [itemX] (itemZ : stackZ)
runHanoi moves ()