Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, it was not "easy", as it doesn't work, unfortunately. <code>halve([1,2,3]-[],A,B)</code> doesn't work; <code>halve_pom([1,2,3]-[],A,B)</code> doesn't work either. Also, it is unclear which of the splitting schemes you prefer, <code>[1,2,3,4,5,6,7] -&gt; ([1,3,5,7] , [2,4,6])</code> or <code>-&gt; ([1,2,3,4] , [5,6,7])</code>.</p> <p>If your <code>halve</code> predicate worked, all you'd have left to do is to define <code>merge</code>, which would merge the two halves of a list, <em>in order</em>. Then,</p> <pre><code>mergesort(A,S):- halve(A,B-[],C-[]), mergesort(B,SB), mergesort(C,SC), merge(SB,SC,S-[]). </code></pre> <p>Note that you probably would call it with a normal list <code>A</code>, i.e. the <code>halve</code> predicate should expect its first argument as a normal list (i.e. not a difference-list).</p> <p>See also <a href="https://stackoverflow.com/questions/15456014/what-does-the-symbol-mean-in-prolog-when-dealing-with-lists">What does the &quot;-&quot; symbol mean in Prolog when dealing with lists?</a> . The <code>'-'</code> is unnecessary; instead, its two constituents should be used as two separate arguments to a predicate.</p> <hr> <p>So, one way to code <code>halve</code> is</p> <pre><code>halve([A,B|C],[A|X]-ZX,[B|Y]-ZY):- halve(C,X-ZX,Y-ZY). halve([A],[A|X]-X,Y-Y). halve([],X-X,Y-Y). </code></pre> <p>The same approach can be used to code <code>merge</code>:</p> <pre><code>merge(SB,SC,S-Z):- SB=[A|B], SC=[C|D], A=&lt;C, S=[A|T], merge(B,SC,T-Z). merge(SB,SC,S-Z):- SB=[A|B], SC=[C|D], A&gt;C, ... , ... . merge(SB,SC,S-Z):- SB=[A|B], SC=[], ... , ... . merge(SB,SC,S-Z):- SB=[], SC=[C|D], S=[C|T], ... . merge([],[],Z-Z). </code></pre>
 

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