3

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)),[])
Mehmet
  • 3,301
  • 8
  • 26
  • 36
1775
  • 661
  • 2
  • 11
  • 15
  • 2
    If you got a solution to your problem, consider going back and marking [Daniel Fisher's excellent answer](http://stackoverflow.com/a/12760586/237428) as the "accepted answer" by clicking the outline of the check mark. [As the FAQ says](http://stackoverflow.com/faq#howtoask), this lets other people know both (a) that this question was solved, and (b) that that specific answer solved it. – Antal Spector-Zabusky Oct 07 '12 at 20:29
  • Regarding your next assignment, perhaps you need to think very hard about why you were set the hanoi problem first. I think it's about recursion in a deep way. – AndrewC Oct 08 '12 at 00:31

2 Answers2

4
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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • Thank you !!! I saw that and I already fix. But now this program solves for n = 2 but not for other values ​​(the answer is the same for n = 0). The change for Hanoi was : 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)= swapLOI(hanoi(swapLOI(hanoi(swapLID(moveLOD(swapLID(((o,n),i,d),s))))))) – 1775 Oct 06 '12 at 14:46
  • Please edit your question to add the revised code (don't replace the old code), it's practically impossible to decipher it in comments. – Daniel Fischer Oct 06 '12 at 14:59
  • Danil, thanks again for the explanation. I understood what you meant, what you said I should do (move n-1) this is done in function moveLOD,swapLID and swapLOI that I tried implementing in hanoi (((o,n),i,d),s). So which correct form (which solves the towers of Hanoi) that uses the functions moveLOD, swapLOI,wapLID you suggest ? I thought of several ways to resolve hanoi for n>1 but I didn't get a generic result for n> 1. Can you help me please? – 1775 Oct 06 '12 at 19:08
  • Daniel,I didn't quite understand what you mean. The objective is to implement tower of Hanoi with these functions (moveLOD,swapLID,swapLOD). What more functions and what parameters(arguments) to create no matter. But in Tower of Hanoi you take one disc at a time and makes the move that disk to another pin. So it is only necessary to know how many disks have each pin. since you will decrease one by one and moving until all disks are on target pin. How would the function to do what you described in sequence 1-7? And where show movements which made ​​a pin to another? – 1775 Oct 06 '12 at 21:00
  • "So it is only necessary to know how many disks have each pin." No, that's not enough. Once some disks have reached their final destination, they must not be moved again. If all you know is how many disks are on each pin, you cannot prevent that, because you don't know which and how many disks shall stay put. If you have an argument telling you how many disks to move from the origin to the destination, it's easy. – Daniel Fischer Oct 06 '12 at 21:24
  • I understood what you meant. But I dont see how it's 'easy' to implement, I tried and I could not. How would the function? – 1775 Oct 06 '12 at 23:05
  • `hanoi 0 configuration = configuration; hanoi 1 configuration = moveLOD configuration; hanoi n configuration = let conf1 = hanoi (n-1) $ swapLID configuration; conf2 = ... in conf3`. I'd rather not solve your entire homework, but that should get you started. – Daniel Fischer Oct 06 '12 at 23:20
3

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")]

adRn
  • 76
  • 5
  • I think it would be better to give meaningful names. Presumably `a b c` mean `from to using`, but there's no way to tell without reading the RHS. – Ami Tavory Apr 02 '16 at 14:19
  • 1
    That doesn't make sense, considering the value that represents "from" or "to" is swapped around within the function. Also, this style (single-char var names) is typical in Haskell. Also, this was taken (without attribution!) from https://rosettacode.org/wiki/Towers_of_Hanoi#Haskell – striking Apr 27 '16 at 19:47
  • Since I didn't find this solution clear, I'm adding my understanding which I hope will help future readers. To solve the problem of moving n discs from a to b, first move n-1 discs from a to c using b, then move the nth disc from a to b then move n-1 discs from c to b using a. This is exactly what @Ami Tavory was getting at. Also useful to read the statement of the problem (and solution) here (exercise 5 http://www.seas.upenn.edu/~cis194/spring13/hw/01-intro.pdf). – Ben Aug 03 '16 at 22:15