HASKELL : Solving Towers of Hanoi

和自甴很熟 提交于 2020-01-04 02:12:12

问题


The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID.

MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. Additionally a string with information about the movement is piling on list of strings.

type Pin = (Char, Int)        -- Represents a rod, named for a character and the number of disks in it.
type Plate = (Pin, Pin, Pin)  -- Represents the configuration of the three rods.(Origin,Intermediate,Destination
type Log = (Plate, [String])  -- Represents a state formed by the configuration of rods and a list of strings that will record the movements made by the algorithm.


moveLOD :: Log -> Log
moveLOD (((o,n), i ,(d,k)),s) = (((o,n-1), i ,(d,k+1)), (o:" -> " ++ [d]):s)

-- swapLOI: Change the positions of the origin rods and intermediate rods.
swapLOI:: Log->Log
swapLOI ((o,i,d),s) = ((i,o,d),s) 

-- swapoLID : Change the positions of the intermediate rods and destination rods.
swapLID:: Log->Log
swapLID ((o,i,d),s) = ((o,d,i),s)

hanoi :: Log -> Log
hanoi:: Int->Log->[String]
hanoi 1 log = transformaLista(moveLOD log)
hanoi n log = hanoi (n-1) (swapLID log) ++ hanoi 1 log ++ hanoi (n-1) (swapLOI(log))

changeToList::Log->[String]
changeToList(p,s) = s

callHanoi:: Int->[String]
callHanoi n = hanoi n ((('O',n),('I',0),('D',0)),[])

回答1:


hanoi :: Log -> Log
hanoi ((('o',0),i,d),s) = ((('o',0),('i',0),('d',0)), [])
hanoi ((('o',1),i,d),s) = moveLOD((('o',1),i,d),s)
hanoi ((('o',n),i,d),s)= hanoi(swapLOI(hanoi(swapLOI(swapLID(moveLOD(swapLID((('o',n),i,d),s)))))))

only defines the function for arguments where the Char in the first Pin of the Plate is 'o', you also need to provide equations for when the character is something else.

When an argument not matching any of the patterns for which there is a defining equation is received, a "Non-exhaustive pattern" error is raised. The only way to fix it is to provide equations for the remaining patterns.

In your revised code, first, your treatment of the case where the origin pin is empty is incorrect,

hanoi (((o,0),i,d),s) = ((('o',0),('i',0),('d',0)),[])

means that whenever this case applies the result is the same, regardless of what d and i are. When hanoi is called from chamahanoi with an argument greater than 2, at some time the origin pole becomes empty, and since above that in the call chain are only hanoi and swapLOI, that constant result bubbles up. You get the correct result for n == 2 (n == 1 is directly solved by the second equation) since the recursive calls to hanoi then both have only one disk on the origin pole.

That case should be

hanoi (((o,0),i,d),s) = (((o,0),i,d),s)

That still doesn't produce correct results (wrong sequence of moves), since the recursion in the general case is wrong.

You

  • move the top disk to the intermediate pin (swapLID . moveLOD . swapLID);
  • then move the remaining disks to the destination (hanoi), but that isn't allowed since the smallest disk is on the intermediate pin and so no other disk may be placed there;
  • finally, move the disk(s) from the intermediate pin to the destination using the (now empty) origin pin as intermediate.

You should

  • move n-1 disks from the origin to the intermediate pin,
  • then move the bottom (largest) disk to the destination,
  • finally, move the n-1 disks from the intermediate to the destination.

I don't see an easy way to do that without an extra argument keeping track of how many disks to move. Consider a four-disk game. First, the top three disks are moved to the intermediate pin, then the bottom disk is moved to the destination pin. Now the task is to move the three disks from the intermediate pin to the destination pin, using the origin pin as helper.

The correct way is the sequence

  1. i -> d (([],[1,2,3],[4]) -> ([],[2,3],[1,4]))
  2. i -> o (([],[2,3],[1,4]) -> ([2],[3],[1,4]))
  3. d -> o (([2],[3],[1,4]) -> ([1,2],[3],[4]))
  4. i -> d (([1,2],[3],[4]) -> ([1,2],[],[3,4]))
  5. o -> i (([1,2],[],[3,4]) -> ([2],[1],[3,4]))
  6. o -> d (([2],[1],[3,4]) -> ([],[1],[2,3,4]))
  7. i -> d (([],[1],[2,3,4]) -> ([],[],[1,2,3,4]))

After step 2, the original destination pin becomes the pin from which disks (well, one) are to be moved to o, but the lowest of those shall not be moved in this situation. How could that be achieved if the only information is how many disks are on each pin, and from where to where disks shall be moved?

If you change the type of hanoi to

hanoi :: Int -> Log -> Log

and call it

chamahanoi n = hanoi n ((('o',n),('i',0),('d',0)),[])

it is easy to implement.

If you don't want to do that, or are not allowed to, you could either keep track of the sizes on each pin, and only move disks onto larger ones, or you could sneakily remove and add disks at the appropriate pins to emulate that restriction, but that would be hard to distinguish from cheating without proper explanation.




回答2:


If it helps anyone, here's another towers of hanoi algorithm:

hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a

For example, hanoi 2 "a" "b" "c" == [("a","c"), ("a","b"), ("c","b")]



来源:https://stackoverflow.com/questions/12760285/haskell-solving-towers-of-hanoi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!