Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created September 22, 2014 00:38
Show Gist options
  • Save gcmurphy/8a5e49d18cd51aea406a to your computer and use it in GitHub Desktop.
Save gcmurphy/8a5e49d18cd51aea406a to your computer and use it in GitHub Desktop.
CS194 Homework 2 - Towers of Hanoi
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