Hanoi towers pattern12/29/2023 move the top disk to the intermediate pin ( swapLID.That still doesn't produce correct results (wrong sequence of moves), since the recursion in the general case is wrong. 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. ![]() 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. Means that whenever this case applies the result is the same, regardless of what d and i are. 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)),) The only way to fix it is to provide equations for the remaining patterns. 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. 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. Hanoi 1 log = transformaLista(moveLOD log) ![]() swapoLID : Change the positions of the intermediate rods and destination rods. swapLOI: Change the positions of the origin rods and intermediate rods. Type Log = (Plate, ) - Represents a state formed by the configuration of rods and a list of strings that will record the movements made by the algorithm. Type Plate = (Pin, Pin, Pin) - Represents the configuration of the three rods.(Origin,Intermediate,Destination type Pin = (Char, Int) - Represents a rod, named for a character and the number of disks in it. Additionally a string with information about the movement is piling on list of strings. ![]() MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |