Created
September 22, 2014 00:38
-
-
Save gcmurphy/8a5e49d18cd51aea406a to your computer and use it in GitHub Desktop.
CS194 Homework 2 - Towers of Hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
type Peg = String | |
type Move = (Peg, Peg) | |
{- | |
| Towers of hanoi solution | |
>>> hanoi 2 "a" "b" "c" | |
[("a","c"),("a","b"),("c","b")] | |
move n-1 disc from a to c using b as temporary storage | |
move the top disc from a to b | |
move n-1 discs from c to b using a as a temporary storage | |
-} | |
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] | |
hanoi 0 _ _ _ = [] | |
hanoi n a b c = hanoi (n-1) a c b ++ [(a, b)] ++ hanoi (n-1) c b a | |
main :: IO() | |
main = do | |
putStrLn "Enter number of disks:" | |
n <- getLine | |
print $ hanoi (read n) "a" "b" "c" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment