Created
February 24, 2015 07:32
-
-
Save elvinio/4115893d9e62fc9a130f to your computer and use it in GitHub Desktop.
Solving the Tower of Hanoi in Haskell
This file contains hidden or 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
hanoi 0 _ _ _ = [] | |
hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a | |
han 0 _ _ _ = [] | |
han n a b c = han (n-1) a c b ++ [(a,c)] ++ han (n-1) b a c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ghci> han 3 "A" "B" "C"
[("A","C"),("A","B"),("C","B"),("A","C"),("B","A"),("B","C"),("A","C")]
ghci> hanoi 4 "A" "B" "C"
[("A","C"),("A","B"),("C","B"),("A","C"),("B","A"),("B","C"),("A","C"),("A","B"),("C","B"),("C","A"),("B","A"),("C","B"),("A","C"),("A","B"),("C","B")]