<div dir="ltr">Hi Saqib,<div><br></div><div>perhaps something like this?</div><div><br></div><div><br></div><div>







<p class="gmail-p1"><span class="gmail-s1">divide :: [Int] -> [[Int]]                                                                                                                                    </span></p>
<p class="gmail-p1"><span class="gmail-s1">divide []     = []                                                                                                                                            </span></p>
<p class="gmail-p1"><span class="gmail-s1">divide (x:xs) = divideAux xs [] [x] x                                                                                                                         </span></p>
<p class="gmail-p2"><span class="gmail-s1">                                                                                                                                                              </span></p>
<p class="gmail-p1"><span class="gmail-s1">divideAux :: [Int] -> [[Int]] -> [Int] -> Int -> [[Int]]                                                                                                      </span></p>
<p class="gmail-p1"><span class="gmail-s1">divideAux [] result current max        = result ++ [current]                                                                                                     </span></p>
<p class="gmail-p1"><span class="gmail-s1">divideAux (x:xs) result current max = if x - max > 1                                                                                                          </span></p>
<p class="gmail-p1"><span class="gmail-s1">                                                              then divideAux xs (result ++ [current]) [x] x                                                                        </span></p>
<p class="gmail-p1"><span class="gmail-s1">                                                              else divideAux xs result (current ++ [x]) x</span></p><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">This can of course be generalised to other types (as per Joel's requirement) just by replacing the check on line 7, i.e., 'x - max > 1'.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Happy to explain/talk about the code if you'd like me to :-)</div><div class="gmail_extra"><br></div><div class="gmail_extra">Cheers,</div><div class="gmail_extra"><br></div><div class="gmail_extra">Carlo</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 13, 2017 at 7:14 PM, Joel Neely <span dir="ltr"><<a href="mailto:joel.neely@gmail.com" target="_blank">joel.neely@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:georgia,serif;font-size:small">Had a chance to chat with ghci, so earlier conjecture not confirmed:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div style="font-size:small"><p class="gmail-m_4075879426117686176gmail-p1"><span class="gmail-m_4075879426117686176gmail-s1"><font face="monospace, monospace">Prelude Data.List> groupBy (\x y -> x == y-1) [1,2,3,7,8,10,11,12]</font></span></p></div><div style="font-size:small"><p class="gmail-m_4075879426117686176gmail-p1"><font face="monospace, monospace">[[1,2],[3],[7,8],[10,11],[12]]</font></p></div></blockquote><div style="font-family:georgia,serif;font-size:small">







<p class="gmail-m_4075879426117686176gmail-p1">So close but no cigar.</p></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="gmail-h5">On Fri, Jan 13, 2017 at 10:05 AM, Saqib Shamsi <span dir="ltr"><<a href="mailto:shamsi.saqib@gmail.com" target="_blank">shamsi.saqib@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="gmail-h5"><div dir="ltr"><div><div><div><div><div><div><div>Hi,<br><br></div>The problem that I wish to solve is to divide a (sored) list of integers into sublists such that each sublist contains numbers in consecutive sequence.<br><br></div><div>For example,<br></div><div><b>Input:</b> [1,2,3,7,8,10,11,12]<br></div><b>Output:</b> [[1,2,3],[7,8],[10,11,12]]<br><br></div>I have written the following code and it does the trick.<br><br><span style="font-family:monospace,monospace">-- Take a list and divide it at first point of non-consecutive number encounter<br>divide :: [Int] -> [Int] -> ([Int], [Int])<br>divide first [] = (first, [])<br>divide first second = if (last first) /= firstSecond - 1 then (first, second)<br>                      else divide (first ++ [firstSecond]) (tail second)<br>                      where firstSecond = head second<br><br><br>-- Helper for breaking a list of numbers into consecutive sublists<br>breakIntoConsecsHelper :: [Int] -> [[Int]] -> [[Int]]<br>breakIntoConsecsHelper [] [[]] = [[]]<br>breakIntoConsecsHelper lst ans = if two == [] then ans ++ [one]<br>                              <wbr>   else ans ++ [one] ++ breakIntoConsecsHelper two []<br>                              <wbr>   where<br>                              <wbr>        firstElem = head lst<br>                              <wbr>        remaining = tail lst<br>                              <wbr>        (one, two) = divide [firstElem] remaining<br><br><br>-- Break the list into sublists of consective numbers<br></span></div><div><span style="font-family:monospace,monospace">breakIntoConsecs :: [Int] -> [[Int]]<br>breakIntoConsecs lst = breakIntoConsecsHelper lst [[]]<br><br></span></div><div><span style="font-family:monospace,monospace">-- Take the tail of the result given by the function above to get the required list of lists.</span><br></div><div><br></div>However, I was wondering if there was a better way of doing this. Any help would be highly appreciated.<br><br></div>Thank you.<br></div>Best Regards,<br></div>Saqib Shamsi<br></div>
<br></div></div><span class="gmail-">______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br></span></blockquote></div><span class="gmail-"><br><br clear="all"><div><br></div>-- <br><div class="gmail-m_4075879426117686176gmail_signature">Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato</div>
</span></div>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Carlo Matteo Scalzo<br><br>Mobile: +44 (0) 7934 583582<br><br>E-mail: <a href="mailto:cmscalzo@gmail.com" target="_blank">cmscalzo@gmail.com</a></div>
</div></div></div>