Skip to content

Instantly share code, notes, and snippets.

@callerobertsson
Created February 22, 2015 00:42
Show Gist options
  • Save callerobertsson/dc513ffd8fda593d6305 to your computer and use it in GitHub Desktop.
Save callerobertsson/dc513ffd8fda593d6305 to your computer and use it in GitHub Desktop.
Haskell: Tower of Hanoi
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n - 1) a c b ++ [(a, c)] ++ hanoi (n - 1) b a c
main = do
print $ hanoi 1 "a" "b" "c"
print $ hanoi 2 "a" "b" "c"
print $ hanoi 3 "a" "b" "c"
print $ hanoi 4 "a" "b" "c"
print $ length (hanoi 20 "a" "b" "c")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment