Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm on chapter 8, so I'm no old hand, but I'd prefer</p> <pre><code>areListsEqual x:xs y:ys = (x == y) &amp;&amp; (areListsEqual xs ys) areListsEqual [] [] = True areListsEqual _ _ = False </code></pre> <p>It seems a bit more in line with Haskell style.</p> <p>Similarly,</p> <pre><code>charlieSort [] = [] charlieSort (x:[]) = [x] charlieSort (x1:x2:xs) = blah blah </code></pre> <p>swapPairIfNeed works as is because you only call it with first and second as its arguments (in that order), but you probably meant</p> <pre><code>swapPairIfNeed a b = if (length a &gt;= length b) then [b, a] else [a, b] </code></pre> <p>In fact, I prefer the third case of charlieSort to look like</p> <pre><code>charlieSort (x1:x2:xs) = if not (areListsEqual x1:x2:xs wip) then charlieSort wip else wip where swapPairIfNeeded a b = if (length a &gt;= length b) then (b, a) else (a, b) wip = f (swapPairIfNeeded first second) f (a, b) = a : (charlieSort b:xs) </code></pre> <p>I think this was all covered by chapter 3.</p> <p>Now, let's examine the algorithm. Even holding ourselves to bubble sort, there's no need to check the whole list after sorting. Instead, we can swap the first two elements if necessary, then sort the tail of the list. If the head is shorter than the head of the sorted tail, we're done.</p> <pre><code>charlieSort (x1:x2:xs) = if (length a &lt;= length (head sortedTail)) then a : sortedTail else charlieSort (a : sortedTail) where sortedTail = charlieSort (b:xs) (a, b) = if (length x1 &gt;= length x2) then (x2, x1) else (x1, x2) </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