cis194-solutions
cis194-solutions copied to clipboard
Homework1 hanoi4 function has wrong result
hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move]
hanoi4 n src goal tmp1 tmp2
| n <= 0 = []
| n == 1 = [(src, goal)]
{-| n == 2 = [(src, tmp1), (src, goal), (tmp1, goal)]-}
| n == 3 = [(src, tmp1), (src, tmp2), (src, goal), (tmp2, goal), (tmp1, goal)]
| otherwise = hanoi4 (n-1) src tmp1 goal tmp2 ++ hanoi4 1 src goal tmp1 tmp2
++ hanoi4 (n-1) tmp1 goal src tmp2
I try to load it and run length(hanoi4 15 "a" "b" "c" "d")
,the result is 24575
,but expected result is 129
His implementation solves the problem but does not minimize the number of moves. If you want to get the expect result you have to implement this algorithm https://en.wikipedia.org/wiki/Tower_of_Hanoi#Frame–Stewart_algorithm.