A recursive Tower of Hanoi solver implemented in Haskell. This program demonstrates the classic recursive algorithm to move disks between three pegs following the rules of the puzzle.
Make sure GHC is installed. To build and execute:
cd tower_of_hanoi
ghc -O2 tower_of_hanoi.hs -o tower_of_hanoi
./tower_of_hanoi <n>
Example:
cd tower_of_hanoi
ghc -O2 tower_of_hanoi.hs -o tower_of_hanoi
./tower_of_hanoi 3
Output:
'A' --> 'C'
'A' --> 'B'
'C' --> 'B'
'A' --> 'C'
'B' --> 'A'
'B' --> 'C'
'A' --> 'C'
Each line represents a single move from one peg to another.
n
disks.move n s d t
recursively transfers disks from source peg s
to destination peg d
using temporary peg t
.move 1 s d t
) performs a single move.n
; output grows exponentially with larger values.'A'
, 'B'
, and 'C'
.Abhrankan Chakrabarti