<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Here's one that does what you want,
      doesn't require the list to be sorted, and groups together
      consecutive and equal terms:<br>
      <br>
      <tt>groupConsecutive :: (Enum a,Eq a) => [a] -> [[a]]</tt><tt><br>
      </tt><tt>groupConsecutive = foldr go []</tt><tt><br>
      </tt><tt>    where go x ls@(hd@(y:_):yss)</tt><tt><br>
      </tt><tt>            | x == y || x == pred y = (x:hd):yss</tt><tt><br>
      </tt><tt>            | otherwise             = [x]:ls</tt><tt><br>
      </tt><tt>          go x [] = [[x]]</tt><tt><br>
      </tt><tt>          go x ([]:yss) = [x]:yss</tt><tt><br>
      </tt><tt><br>
      </tt>Then<br>
      <tt>> groupConsecutive [1,2,3,7,8,10,11,12]</tt><tt><br>
      </tt><tt>[[1,2,3],[7,8],[10,11,12]]</tt><tt><br>
      </tt><br>
      <tt>> groupConsecutive [1,2,2,3,2,3]</tt><tt><br>
      </tt><tt>[[1,2,2,3],[2,3]]</tt><tt><br>
      </tt><br>
      and<br>
      <tt>> groupConsecutive "bookkeeper understudy"</tt><tt><br>
      </tt><tt>["b","oo","kk","ee","p","e","r","
        ","u","n","de","rstu","d","y"]</tt><tt><br>
      </tt><br>
      The third case of <tt>go</tt> will never be reached. If you use a
      type that is also an instance of Bounded, and if your list
      contains the minimum element of the type, you'll get a runtime
      error on the use of <tt>pred</tt>. For example:<br>
      <br>
      <tt>> groupConsecutive [True,False,True]</tt><tt><br>
      </tt><tt>*** Exception: Prelude.Enum.Bool.pred: bad argument</tt><tt><br>
      </tt><br>
      Graham<br>
      <br>
      On 13-Jan-2017 11:05 AM, Saqib Shamsi wrote:<br>
    </div>
    <blockquote
cite="mid:CAKpYsCGKeWoHttqpZAF7jAkhe++PqLSfwZA820tA=Je8hFiu8g@mail.gmail.com"
      type="cite">
      <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>
                                                   else ans ++ [one] ++
                  breakIntoConsecsHelper two []<br>
                                                   where<br>
                                                        firstElem = head
                  lst<br>
                                                        remaining = tail
                  lst<br>
                                                        (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>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>