Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>hanoi :: Log -&gt; 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))))))) </code></pre> <p>only defines the function for arguments where the <code>Char</code> in the first <code>Pin</code> of the <code>Plate</code> is <code>'o'</code>, you also need to provide equations for when the character is something else.</p> <p>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.</p> <p>In your revised code, first, your treatment of the case where the origin pin is empty is incorrect,</p> <pre><code>hanoi (((o,0),i,d),s) = ((('o',0),('i',0),('d',0)),[]) </code></pre> <p>means that whenever this case applies the result is the same, regardless of what <code>d</code> and <code>i</code> are. When <code>hanoi</code> is called from <code>chamahanoi</code> with an argument greater than 2, at some time the origin pole becomes empty, and since above that in the call chain are only <code>hanoi</code> and <code>swapLOI</code>, that constant result bubbles up. You get the correct result for <code>n == 2</code> (<code>n == 1</code> is directly solved by the second equation) since the recursive calls to <code>hanoi</code> then both have only one disk on the origin pole.</p> <p>That case should be</p> <pre><code>hanoi (((o,0),i,d),s) = (((o,0),i,d),s) </code></pre> <p>That still doesn't produce correct results (wrong sequence of moves), since the recursion in the general case is wrong.</p> <p>You</p> <ul> <li>move the top disk to the intermediate pin (<code>swapLID . moveLOD . swapLID</code>);</li> <li>then move the remaining disks to the destination (<code>hanoi</code>), but that isn't allowed since the smallest disk is on the intermediate pin and so no other disk may be placed there;</li> <li>finally, move the disk(s) from the intermediate pin to the destination using the (now empty) origin pin as intermediate.</li> </ul> <p>You should</p> <ul> <li>move <code>n-1</code> disks from the origin to the intermediate pin,</li> <li>then move the bottom (largest) disk to the destination,</li> <li>finally, move the <code>n-1</code> disks from the intermediate to the destination.</li> </ul> <p>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.</p> <p>The correct way is the sequence</p> <ol> <li><code>i -&gt; d</code> (<code>([],[1,2,3],[4]) -&gt; ([],[2,3],[1,4])</code>)</li> <li><code>i -&gt; o</code> (<code>([],[2,3],[1,4]) -&gt; ([2],[3],[1,4])</code>)</li> <li><code>d -&gt; o</code> (<code>([2],[3],[1,4]) -&gt; ([1,2],[3],[4])</code>)</li> <li><code>i -&gt; d</code> (<code>([1,2],[3],[4]) -&gt; ([1,2],[],[3,4])</code>)</li> <li><code>o -&gt; i</code> (<code>([1,2],[],[3,4]) -&gt; ([2],[1],[3,4])</code>)</li> <li><code>o -&gt; d</code> (<code>([2],[1],[3,4]) -&gt; ([],[1],[2,3,4])</code>)</li> <li><code>i -&gt; d</code> (<code>([],[1],[2,3,4]) -&gt; ([],[],[1,2,3,4])</code>)</li> </ol> <p>After step 2, the original destination pin becomes the pin from which disks (well, one) are to be moved to <code>o</code>, 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?</p> <p>If you change the type of <code>hanoi</code> to</p> <pre><code>hanoi :: Int -&gt; Log -&gt; Log </code></pre> <p>and call it</p> <pre><code>chamahanoi n = hanoi n ((('o',n),('i',0),('d',0)),[]) </code></pre> <p>it is easy to implement.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload