Sunday, April 7, 2013

HASKELL TOWERS OF HANOI

HASKELL TOWERS OF HANOI

REVISED: Wednesday, January 24, 2024




Haskell Towers of Hanoi.

I. HASKELL TOWERS OF HANOI

The Towers of Hanoi is a game or puzzle. It consists of three vertical anchored rods of equal length and diameter, spaced an equal distance apart on a playing board. The puzzle includes a number of disks of different diameters and equal thickness with a hole in their center slightly larger than the diameter of the rods. The disks can slide onto any of the three rods. The puzzle starts with all the disks in a neat conical stack, the smallest disk at the top, in ascending order of size on one rod, the rod to the far left facing the player. The far right vertical rod can be used as a temporary resting place for the disks as they are moved back and forth between the three rods. The objective of the puzzle is to eventually move the entire stack of disks from the rod on the far left, to the rod in the middle, obeying the following rules:

A. Only one disk may be moved at a time.

B. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.

C. No disk may be placed on top of a smaller disk.

A. SKELETON

main :: IO ()
main = do  

B. FLESH ON BONES

1. SAVE

Use your editor to save the following TowersOfHanoi.hs file:

main :: IO ()
main = do
        putStrLn "Given three stationary rods: r1, r2, and r3; enter number of disks you want to move: "
        a <- readLn
        let d = hanoi a "r1" "r2" "r3"
        putStrLn "Towers of Hanoi Disk Movement Solution: "
        putStr d

-- Towers of Hanoi
hanoi :: Integer -> String -> String -> String -> String
hanoi 1 r1 r2 r3 = "From " ++ r1 ++ " to " ++ r2 ++ "\n"
hanoi n r1 r2 r3 =(hanoi (n-1) r1 r3 r2) ++ (hanoi 1 r1 r2 r3) ++ (hanoi (n-1) r3 r2 r1)

2. LOAD

As shown below, load the above TowersOfHanoi.hs file into GHCi:

Prelude>  :load TowersOfHanoi
[1 of 1] Compiling Main             ( TowersOfHanoi.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Now that we have loaded Main, we can run functions in GHCi that were defined in Main.

3. RUN

As shown below, run the function main, in GHCi:

Prelude>  main
Given three stationary rods: r1, r2, and r3; enter number of disks you want to move: 
3
Towers of Hanoi Disk Movement Solution: 
From r1 to r2
From r1 to r3
From r2 to r3
From r1 to r2
From r3 to r1
From r3 to r2
From r1 to r2
Prelude>  

4. COMPILE 

As shown below, compile TowersOfHanoi.hs in GHC:

Prelude>  :! ghc --make "*TowersOfHanoi"
[1 of 1] Compiling Main             ( TowersOfHanoi.hs, TowersOfHanoi.o )
Linking TowersOfHanoi.exe ...
Prelude>

The compile is shown so you know it compiles without error.

C. COMMENTS

Use the program shown above as an example. Rewrite the example and make it your own program.

Repeat 1. thru 3. above until your new program works the way you want it to work. Each time you make changes in your editor to your program, make sure you save the file and load it in GHCi.

II. REFERENCE

Haskell Programming by Sankha Mukherjee

Haskell Tutorial 3 - Recursion

In this tutorial, you have been introduced to a Haskell recursive solution for the Towers of Hanoi.