Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't generally want <em>tail-recursion</em> in Haskell. What you do want, is <em>productive corecursion</em> (see also <a href="https://softwareengineering.stackexchange.com/a/196827/58561">this</a>), describing what in <a href="http://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs" rel="nofollow noreferrer">SICP</a> is called an <em>iterative process</em>.</p> <p>You can fix the <em>type inconsistency</em> in your function by enclosing initial input in a list. In your example</p> <pre><code>[1, 2, 3, 4, 5] -&gt; [[1, 3, 4], [2, 5]] -&gt; [[1, 3], [4], [2], [5]] </code></pre> <p>only the first arrow is inconsistent, so change it into</p> <pre><code>[[1, 2, 3, 4, 5]] -&gt; [[1, 3, 4], [2, 5]] -&gt; [[1, 3], [4], [2], [5]] </code></pre> <p>which illustrates the process of iteratively applying <code>concatMap splitList1</code>, where</p> <pre><code> splitList1 xs | null $ drop 1 xs = [xs] | magic a b &gt; 0 = [a,b] -- (B) | otherwise = [xs] where (a,b) = splitSomeHow xs </code></pre> <p>You want to stop if no <code>(B)</code> case was fired at a certain iteration. </p> <p>(edit: removed the intermediate version)</p> <p>But it is much better to produce the portions of the output that are ready, as soon as possible:</p> <pre><code>splitList :: [Int] -&gt; [[Int]] splitList xs = g [xs] -- explicate the stack where g [] = [] g (xs : t) | null $ drop 1 xs = xs : g t | magic a b &gt; 0 = g (a : b : t) | otherwise = xs : g t where (a,b) = splitSomeHow xs -- magic a b = 1 -- splitSomeHow = splitAt 2 </code></pre> <p>Don't forget to compile with <code>-O2</code> flag.</p>
 

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