Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: safe versions of pred and succ? (Michael Orlitzky) 2. Re: Beginners Digest, Vol 103, Issue 8 (Saqib Shamsi) ---------------------------------------------------------------------- Message: 1 Date: Sun, 15 Jan 2017 14:05:26 -0500 From: Michael Orlitzky <mich...@orlitzky.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] safe versions of pred and succ? Message-ID: <aa1d2a8e-0290-5828-da55-47a2a812a...@orlitzky.com> Content-Type: text/plain; charset=utf-8 On 01/14/2017 02:35 AM, Graham Gill wrote: > > Do I need two different versions of f, one for Bounded a and one for > non-Bounded a? Is there a more elegant way to take care of this problem? > I don't know much about all of the type magic available in GHC. > You probably want your own typeclass instead of Enum, even if it means generating a bunch of very boring instances of it for the types you want "f" to work on. The fact that "pred" should throw a runtime error on "minBound" is a documented fact of the Enum typeclass, and you should stay far far away from such things in your own code. Besides that, there's a weird interaction between the semantic meaning of Enum and Bounded. For example, here's a perfectly valid enumeration of boolean values: True, False, True, False, ... In your case it would be fine to have (pred False) == True, but instead you get a runtime error thanks to the Bounded instance. So being Bounded rules out some otherwise valid (and fine for your purposes) Enum instances. Your "f" should also work on a singleton type: ghci> data Foo = Foo deriving (Eq,Show) ghci> instance Enum Foo where toEnum _ = Foo; fromEnum _ = 0; ghci> groupConsecutive [Foo,Foo,Foo,Foo] [[Foo,Foo,Foo,Foo]] But any Bounded instance for Foo would mess that up. Basically, the pre-existing Enum instances aren't exactly what you want. ------------------------------ Message: 2 Date: Mon, 16 Jan 2017 11:16:27 +0530 From: Saqib Shamsi <shamsi.sa...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Beginners Digest, Vol 103, Issue 8 Message-ID: <CAKpYsCGy2LUfBrdLAdi=7GJ=hrzhnqymra-ohbk60wxl2op...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello Francesco, Sorry for the late reply. Yes, your solution is elegant and does what I wanted. This is what I did -- zipped is the list of tuples formed as you mentioned λ> groupBy (\x y -> (snd x) == (snd y)) zipped [[(1,0),(2,0),(3,0)],[(7,3),(8,3)],[(10,4),(11,4),(12,4)]] λ> [[fst tpl | tpl <- x] | x <- it] [[1,2,3],[7,8],[10,11,12]] This works perfectly fine. Thank you. Regards, Saqib Shamsi On 14 January 2017 at 00:27, <beginners-requ...@haskell.org> wrote: > Send Beginners mailing list submissions to > beginners@haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-requ...@haskell.org > > You can reach the person managing the list at > beginners-ow...@haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Better Code (Saqib Shamsi) > 2. Re: Better Code (Francesco Ariis) > 3. Re: Better Code (Joel Neely) > 4. Re: Better Code (Joel Neely) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 13 Jan 2017 21:35:54 +0530 > From: Saqib Shamsi <shamsi.sa...@gmail.com> > To: beginners@haskell.org > Subject: [Haskell-beginners] Better Code > Message-ID: > <CAKpYsCGKeWoHttqpZAF7jAkhe++PqLSfwZA820tA=Je8hFiu8g@mail. > gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hi, > > 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. > > For example, > *Input:* [1,2,3,7,8,10,11,12] > *Output:* [[1,2,3],[7,8],[10,11,12]] > > I have written the following code and it does the trick. > > -- Take a list and divide it at first point of non-consecutive number > encounter > divide :: [Int] -> [Int] -> ([Int], [Int]) > divide first [] = (first, []) > divide first second = if (last first) /= firstSecond - 1 then (first, > second) > else divide (first ++ [firstSecond]) (tail second) > where firstSecond = head second > > > -- Helper for breaking a list of numbers into consecutive sublists > breakIntoConsecsHelper :: [Int] -> [[Int]] -> [[Int]] > breakIntoConsecsHelper [] [[]] = [[]] > breakIntoConsecsHelper lst ans = if two == [] then ans ++ [one] > else ans ++ [one] ++ > breakIntoConsecsHelper two [] > where > firstElem = head lst > remaining = tail lst > (one, two) = divide [firstElem] > remaining > > > -- Break the list into sublists of consective numbers > breakIntoConsecs :: [Int] -> [[Int]] > breakIntoConsecs lst = breakIntoConsecsHelper lst [[]] > > -- Take the tail of the result given by the function above to get the > required list of lists. > > However, I was wondering if there was a better way of doing this. Any help > would be highly appreciated. > > Thank you. > Best Regards, > Saqib Shamsi > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: <http://mail.haskell.org/pipermail/beginners/ > attachments/20170113/3f37c4a5/attachment-0001.html> > > ------------------------------ > > Message: 2 > Date: Fri, 13 Jan 2017 17:55:16 +0100 > From: Francesco Ariis <fa...@ariis.it> > To: beginners@haskell.org > Subject: Re: [Haskell-beginners] Better Code > Message-ID: <20170113165516.gb23...@casa.casa> > Content-Type: text/plain; charset=utf-8 > > On Fri, Jan 13, 2017 at 09:35:54PM +0530, Saqib Shamsi wrote: > > 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. > > > > For example, > > *Input:* [1,2,3,7,8,10,11,12] > > *Output:* [[1,2,3],[7,8],[10,11,12]] > > > > [...] > > > > However, I was wondering if there was a better way of doing this. Any > help > > would be highly appreciated. > > Hello Saquib, > you could try using a 'trick' like this: > > λ> zipWith (-) [1,2,3,7,8,10,11,12] (enumFrom 1) > [0,0,0,3,3,4,4,4] > > Now you have an 'helper' list which can be glued to the first one > with zip > > λ> zip [1,2,3,7,8,10,11,12] it > [(1,0),(2,0),(3,0),(7,3),(8,3),(10,4),(11,4),(12,4)] > > and now grouped by using `groupBy` in Data.List. > > Does that help? > > > ------------------------------ > > Message: 3 > Date: Fri, 13 Jan 2017 11:40:57 -0600 > From: Joel Neely <joel.ne...@gmail.com> > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell <beginners@haskell.org> > Subject: Re: [Haskell-beginners] Better Code > Message-ID: > <CAEEzXAjPaZ+F3-+3FDMy+DRdXX-Qvrqvw6=HWgHiJSReubWb6g@mail. > gmail.com> > Content-Type: text/plain; charset="utf-8" > > The "helper list" technique is ingenious, but seems very specific to Int as > the type within the list. > > I have had other tasks that seem to fit a more generalized problem > statement, of which the original question appears to me as special case: > > Given a criterion of type a -> a -> Bool and a list [a], produce a list of > lists [[a]] in which sub-lists are made up of consecutive elements from the > original list that satisfy the criterion. > > It appears to me that List.groupBy may meet that need, but I'm not able to > verify that at the moment (and would be glad of feedback). > > -jn- > > > > > > On Fri, Jan 13, 2017 at 10:55 AM, Francesco Ariis <fa...@ariis.it> wrote: > > > On Fri, Jan 13, 2017 at 09:35:54PM +0530, Saqib Shamsi wrote: > > > 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. > > > > > > For example, > > > *Input:* [1,2,3,7,8,10,11,12] > > > *Output:* [[1,2,3],[7,8],[10,11,12]] > > > > > > [...] > > > > > > However, I was wondering if there was a better way of doing this. Any > > help > > > would be highly appreciated. > > > > Hello Saquib, > > you could try using a 'trick' like this: > > > > λ> zipWith (-) [1,2,3,7,8,10,11,12] (enumFrom 1) > > [0,0,0,3,3,4,4,4] > > > > Now you have an 'helper' list which can be glued to the first one > > with zip > > > > λ> zip [1,2,3,7,8,10,11,12] it > > [(1,0),(2,0),(3,0),(7,3),(8,3),(10,4),(11,4),(12,4)] > > > > and now grouped by using `groupBy` in Data.List. > > > > Does that help? > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > -- > Beauty of style and harmony and grace and good rhythm depend on simplicity. > - Plato > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: <http://mail.haskell.org/pipermail/beginners/ > attachments/20170113/35931560/attachment-0001.html> > > ------------------------------ > > Message: 4 > Date: Fri, 13 Jan 2017 13:14:56 -0600 > From: Joel Neely <joel.ne...@gmail.com> > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell <beginners@haskell.org> > Subject: Re: [Haskell-beginners] Better Code > Message-ID: > <CAEEzXAg=a2-xJm+vu0cGA3ETkViZP=PZx8o31oocAaMF5 > fh...@mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > Had a chance to chat with ghci, so earlier conjecture not confirmed: > > Prelude Data.List> groupBy (\x y -> x == y-1) [1,2,3,7,8,10,11,12] > > [[1,2],[3],[7,8],[10,11],[12]] > > So close but no cigar. > > On Fri, Jan 13, 2017 at 10:05 AM, Saqib Shamsi <shamsi.sa...@gmail.com> > wrote: > > > Hi, > > > > 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. > > > > For example, > > *Input:* [1,2,3,7,8,10,11,12] > > *Output:* [[1,2,3],[7,8],[10,11,12]] > > > > I have written the following code and it does the trick. > > > > -- Take a list and divide it at first point of non-consecutive number > > encounter > > divide :: [Int] -> [Int] -> ([Int], [Int]) > > divide first [] = (first, []) > > divide first second = if (last first) /= firstSecond - 1 then (first, > > second) > > else divide (first ++ [firstSecond]) (tail second) > > where firstSecond = head second > > > > > > -- Helper for breaking a list of numbers into consecutive sublists > > breakIntoConsecsHelper :: [Int] -> [[Int]] -> [[Int]] > > breakIntoConsecsHelper [] [[]] = [[]] > > breakIntoConsecsHelper lst ans = if two == [] then ans ++ [one] > > else ans ++ [one] ++ > > breakIntoConsecsHelper two [] > > where > > firstElem = head lst > > remaining = tail lst > > (one, two) = divide [firstElem] > > remaining > > > > > > -- Break the list into sublists of consective numbers > > breakIntoConsecs :: [Int] -> [[Int]] > > breakIntoConsecs lst = breakIntoConsecsHelper lst [[]] > > > > -- Take the tail of the result given by the function above to get the > > required list of lists. > > > > However, I was wondering if there was a better way of doing this. Any > help > > would be highly appreciated. > > > > Thank you. > > Best Regards, > > Saqib Shamsi > > > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > > -- > Beauty of style and harmony and grace and good rhythm depend on simplicity. > - Plato > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: <http://mail.haskell.org/pipermail/beginners/ > attachments/20170113/df78caf6/attachment.html> > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 103, Issue 8 > ***************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170116/5e8ef3ea/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 103, Issue 11 ******************************************