<div dir="ltr"><div><div>Hello Francesco,<br><br></div>Sorry for the late reply. Yes, your solution is elegant and does what I wanted. This is what I did<br><br></div>-- zipped is the list of tuples formed as you mentioned<br><br><div>λ> groupBy (\x y -> (snd x) == (snd y)) zipped<br>[[(1,0),(2,0),(3,0)],[(7,3),(8,3)],[(10,4),(11,4),(12,4)]]<br><br>λ> [[fst tpl | tpl <- x] | x <- it]<br>[[1,2,3],[7,8],[10,11,12]]<br><br></div><div>This works perfectly fine.<br><br></div><div>Thank you.<br></div><div>Regards,<br></div><div>Saqib Shamsi<br></div><div><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 14 January 2017 at 00:27,  <span dir="ltr"><<a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send Beginners mailing list submissions to<br>
        <a href="mailto:beginners@haskell.org">beginners@haskell.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<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>
or, via email, send a message with subject or body 'help' to<br>
        <a href="mailto:beginners-request@haskell.org">beginners-request@haskell.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:beginners-owner@haskell.org">beginners-owner@haskell.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Beginners digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
   1.  Better Code (Saqib Shamsi)<br>
   2. Re:  Better Code (Francesco Ariis)<br>
   3. Re:  Better Code (Joel Neely)<br>
   4. Re:  Better Code (Joel Neely)<br>
<br>
<br>
------------------------------<wbr>------------------------------<wbr>----------<br>
<br>
Message: 1<br>
Date: Fri, 13 Jan 2017 21:35:54 +0530<br>
From: Saqib Shamsi <<a href="mailto:shamsi.saqib@gmail.com">shamsi.saqib@gmail.com</a>><br>
To: <a href="mailto:beginners@haskell.org">beginners@haskell.org</a><br>
Subject: [Haskell-beginners] Better Code<br>
Message-ID:<br>
        <CAKpYsCGKeWoHttqpZAF7jAkhe++<wbr>PqLSfwZA820tA=<a href="mailto:Je8hFiu8g@mail.gmail.com">Je8hFiu8g@mail.<wbr>gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hi,<br>
<br>
The problem that I wish to solve is to divide a (sored) list of integers<br>
into sublists such that each sublist contains numbers in consecutive<br>
sequence.<br>
<br>
For example,<br>
*Input:* [1,2,3,7,8,10,11,12]<br>
*Output:* [[1,2,3],[7,8],[10,11,12]]<br>
<br>
I have written the following code and it does the trick.<br>
<br>
-- Take a list and divide it at first point of non-consecutive number<br>
encounter<br>
divide :: [Int] -> [Int] -> ([Int], [Int])<br>
divide first [] = (first, [])<br>
divide first second = if (last first) /= firstSecond - 1 then (first,<br>
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] ++<br>
breakIntoConsecsHelper two []<br>
                                 where<br>
                                      firstElem = head lst<br>
                                      remaining = tail lst<br>
                                      (one, two) = divide [firstElem]<br>
remaining<br>
<br>
<br>
-- Break the list into sublists of consective numbers<br>
breakIntoConsecs :: [Int] -> [[Int]]<br>
breakIntoConsecs lst = breakIntoConsecsHelper lst [[]]<br>
<br>
-- Take the tail of the result given by the function above to get the<br>
required list of lists.<br>
<br>
However, I was wondering if there was a better way of doing this. Any help<br>
would be highly appreciated.<br>
<br>
Thank you.<br>
Best Regards,<br>
Saqib Shamsi<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20170113/3f37c4a5/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/<wbr>pipermail/beginners/<wbr>attachments/20170113/3f37c4a5/<wbr>attachment-0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Fri, 13 Jan 2017 17:55:16 +0100<br>
From: Francesco Ariis <<a href="mailto:fa-ml@ariis.it">fa-ml@ariis.it</a>><br>
To: <a href="mailto:beginners@haskell.org">beginners@haskell.org</a><br>
Subject: Re: [Haskell-beginners] Better Code<br>
Message-ID: <20170113165516.GB23022@casa.<wbr>casa><br>
Content-Type: text/plain; charset=utf-8<br>
<br>
On Fri, Jan 13, 2017 at 09:35:54PM +0530, Saqib Shamsi wrote:<br>
> The problem that I wish to solve is to divide a (sored) list of integers<br>
> into sublists such that each sublist contains numbers in consecutive<br>
> sequence.<br>
><br>
> For example,<br>
> *Input:* [1,2,3,7,8,10,11,12]<br>
> *Output:* [[1,2,3],[7,8],[10,11,12]]<br>
><br>
> [...]<br>
><br>
> However, I was wondering if there was a better way of doing this. Any help<br>
> would be highly appreciated.<br>
<br>
Hello Saquib,<br>
    you could try using a 'trick' like this:<br>
<br>
λ> zipWith (-) [1,2,3,7,8,10,11,12] (enumFrom 1)<br>
[0,0,0,3,3,4,4,4]<br>
<br>
Now you have an 'helper' list which can be glued to the first one<br>
with zip<br>
<br>
λ> zip [1,2,3,7,8,10,11,12] it<br>
[(1,0),(2,0),(3,0),(7,3),(8,3)<wbr>,(10,4),(11,4),(12,4)]<br>
<br>
and now grouped by using `groupBy` in Data.List.<br>
<br>
Does that help?<br>
<br>
<br>
------------------------------<br>
<br>
Message: 3<br>
Date: Fri, 13 Jan 2017 11:40:57 -0600<br>
From: Joel Neely <<a href="mailto:joel.neely@gmail.com">joel.neely@gmail.com</a>><br>
To: The Haskell-Beginners Mailing List - Discussion of primarily<br>
        beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Better Code<br>
Message-ID:<br>
        <CAEEzXAjPaZ+F3-+3FDMy+DRdXX-<wbr>Qvrqvw6=<a href="mailto:HWgHiJSReubWb6g@mail.gmail.com">HWgHiJSReubWb6g@mail.<wbr>gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
The "helper list" technique is ingenious, but seems very specific to Int as<br>
the type within the list.<br>
<br>
I have had other tasks that seem to fit a more generalized problem<br>
statement, of which the original question appears to me as special case:<br>
<br>
Given a criterion of type a -> a -> Bool and a list [a], produce a list of<br>
lists [[a]] in which sub-lists are made up of consecutive elements from the<br>
original list that satisfy the criterion.<br>
<br>
It appears to me that List.groupBy may meet that need, but I'm not able to<br>
verify that at the moment (and would be glad of feedback).<br>
<br>
-jn-<br>
<br>
<br>
<br>
<br>
<br>
On Fri, Jan 13, 2017 at 10:55 AM, Francesco Ariis <<a href="mailto:fa-ml@ariis.it">fa-ml@ariis.it</a>> wrote:<br>
<br>
> On Fri, Jan 13, 2017 at 09:35:54PM +0530, Saqib Shamsi wrote:<br>
> > The problem that I wish to solve is to divide a (sored) list of integers<br>
> > into sublists such that each sublist contains numbers in consecutive<br>
> > sequence.<br>
> ><br>
> > For example,<br>
> > *Input:* [1,2,3,7,8,10,11,12]<br>
> > *Output:* [[1,2,3],[7,8],[10,11,12]]<br>
> ><br>
> > [...]<br>
> ><br>
> > However, I was wondering if there was a better way of doing this. Any<br>
> help<br>
> > would be highly appreciated.<br>
><br>
> Hello Saquib,<br>
>     you could try using a 'trick' like this:<br>
><br>
> λ> zipWith (-) [1,2,3,7,8,10,11,12] (enumFrom 1)<br>
> [0,0,0,3,3,4,4,4]<br>
><br>
> Now you have an 'helper' list which can be glued to the first one<br>
> with zip<br>
><br>
> λ> zip [1,2,3,7,8,10,11,12] it<br>
> [(1,0),(2,0),(3,0),(7,3),(8,3)<wbr>,(10,4),(11,4),(12,4)]<br>
><br>
> and now grouped by using `groupBy` in Data.List.<br>
><br>
> Does that help?<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>
<br>
<br>
<br>
--<br>
Beauty of style and harmony and grace and good rhythm depend on simplicity.<br>
- Plato<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20170113/35931560/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/<wbr>pipermail/beginners/<wbr>attachments/20170113/35931560/<wbr>attachment-0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 4<br>
Date: Fri, 13 Jan 2017 13:14:56 -0600<br>
From: Joel Neely <<a href="mailto:joel.neely@gmail.com">joel.neely@gmail.com</a>><br>
To: The Haskell-Beginners Mailing List - Discussion of primarily<br>
        beginner-level topics related to Haskell <<a href="mailto:beginners@haskell.org">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Better Code<br>
Message-ID:<br>
        <CAEEzXAg=a2-xJm+<wbr>vu0cGA3ETkViZP=<a href="mailto:PZx8o31oocAaMF5fhE_Q@mail.gmail.com">PZx8o31oocAaMF5<wbr>fhE_Q@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Had a chance to chat with ghci, so earlier conjecture not confirmed:<br>
<br>
Prelude Data.List> groupBy (\x y -> x == y-1) [1,2,3,7,8,10,11,12]<br>
<br>
[[1,2],[3],[7,8],[10,11],[12]]<br>
<br>
So close but no cigar.<br>
<br>
On Fri, Jan 13, 2017 at 10:05 AM, Saqib Shamsi <<a href="mailto:shamsi.saqib@gmail.com">shamsi.saqib@gmail.com</a>><br>
wrote:<br>
<br>
> Hi,<br>
><br>
> The problem that I wish to solve is to divide a (sored) list of integers<br>
> into sublists such that each sublist contains numbers in consecutive<br>
> sequence.<br>
><br>
> For example,<br>
> *Input:* [1,2,3,7,8,10,11,12]<br>
> *Output:* [[1,2,3],[7,8],[10,11,12]]<br>
><br>
> I have written the following code and it does the trick.<br>
><br>
> -- Take a list and divide it at first point of non-consecutive number<br>
> encounter<br>
> divide :: [Int] -> [Int] -> ([Int], [Int])<br>
> divide first [] = (first, [])<br>
> divide first second = if (last first) /= firstSecond - 1 then (first,<br>
> 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] ++<br>
> breakIntoConsecsHelper two []<br>
>                                  where<br>
>                                       firstElem = head lst<br>
>                                       remaining = tail lst<br>
>                                       (one, two) = divide [firstElem]<br>
> remaining<br>
><br>
><br>
> -- Break the list into sublists of consective numbers<br>
> breakIntoConsecs :: [Int] -> [[Int]]<br>
> breakIntoConsecs lst = breakIntoConsecsHelper lst [[]]<br>
><br>
> -- Take the tail of the result given by the function above to get the<br>
> required list of lists.<br>
><br>
> However, I was wondering if there was a better way of doing this. Any help<br>
> would be highly appreciated.<br>
><br>
> Thank you.<br>
> Best Regards,<br>
> Saqib Shamsi<br>
><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>
><br>
<br>
<br>
--<br>
Beauty of style and harmony and grace and good rhythm depend on simplicity.<br>
- Plato<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20170113/df78caf6/attachment.html" rel="noreferrer" target="_blank">http://mail.haskell.org/<wbr>pipermail/beginners/<wbr>attachments/20170113/df78caf6/<wbr>attachment.html</a>><br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<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>
<br>
------------------------------<br>
<br>
End of Beginners Digest, Vol 103, Issue 8<br>
******************************<wbr>***********<br>
</blockquote></div><br></div></div>