Created
July 15, 2015 18:43
-
-
Save dmgottlieb/56369e01521c61dd00b6 to your computer and use it in GitHub Desktop.
4-tower Tower of Hanoi solutions using μ-recursion in Haskell
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
import Data.List | |
import Data.Ord | |
-- Tower of Hanoi implementation | |
type Peg = String | |
type Move = (Peg, Peg) | |
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] | |
hanoi 0 a b c = [ ] | |
hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a | |
-- Tower of Hanoi with 4 pegs. | |
hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move] | |
hanoi4 0 a b c d = [ ] | |
hanoi4 1 a b c d = [(a, b)] | |
hanoi4 n a b c d = shortest [(hanoi4 (n - k) a d c b ++ hanoi k a b c ++ hanoi4 (n - k) d b a c) | k <- [1..(n-1)]] | |
shortest :: [[a]] -> [a] | |
shortest [] = [] | |
shortest ls = minimumBy (comparing length) ls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On reflection, i think this isn't really μ-recursion. Minimization over finite arguments can be done w/ primitive recursion IIRC.