tag:blogger.com,1999:blog-7094652.post5106940041007316444..comments2024-03-23T14:36:09.980+00:00Comments on Neil Mitchell's Blog (Haskell etc): A simple Haskell functionNeil Mitchellhttp://www.blogger.com/profile/13084722756124486154noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7094652.post-56709859315555494182016-01-17T22:37:18.330+00:002016-01-17T22:37:18.330+00:00Neil: Ah, of course, you are right. I'm now te...Neil: Ah, of course, you are right. I'm now tempted to generalise our implementation in Shaking-up-GHC, just for fun :-)Andrey Mokhovhttps://www.blogger.com/profile/18314567019729554255noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-26687991017525998082016-01-17T21:48:31.183+00:002016-01-17T21:48:31.183+00:00Andrey: Using the Sum Monoid wrapper you can just ...Andrey: Using the Sum Monoid wrapper you can just have any Num, so it's just as powerful as Real. Having push/pop grouped that way would be a very novel use of chunksOfSizeBy, certainly one I hadn't thought of.Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-12298047829204626012016-01-16T16:12:32.803+00:002016-01-16T16:12:32.803+00:00Neil: Semigroup + Ord is a reasonable constraint t...Neil: Semigroup + Ord is a reasonable constraint too, but Real permits negative sizes! This may be useful, when you are never allowed to exceed the size of an intermediate chunk, yet cancellations are allowed. Imagine calling <b>chunksOfSizeBy</b> on a list of <b>data StackOp a = Push a | Pop</b> with weights +1 and -1, respectively.Andrey Mokhovhttps://www.blogger.com/profile/18314567019729554255noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-49363166270701634172016-01-15T12:55:48.276+00:002016-01-15T12:55:48.276+00:00Andrey: Yes, this function was a lot deeper than I...Andrey: Yes, this function was a lot deeper than I expected! With your generalisation, that is quite general - although Semigroup + Ord would work instead of Real too.<br /><br />Daniel: I need split on the outer list, but for the inner piece I only need length. There are a lot of divisions of the Monoid class I never knew about before!Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-36786356361573323722016-01-15T07:35:46.452+00:002016-01-15T07:35:46.452+00:00"The type signature could be generalised to [..."The type signature could be generalised to [a] instead of String, but I would suspect in this context it's more likely for String to be replaced by Text or ByteString, rather than to be used on [Bool]. As a result, sticking to String seems best."<br /><br />Perhaps the function could be implemented in terms of the <a href="http://hackage.haskell.org/package/monoid-subclasses-0.4.1.2/docs/Data-Monoid-Factorial.html#t:StableFactorialMonoid" rel="nofollow">StableFactorialMonoid</a> typeclass from monoid-subclasses. It provides the required methods (length,splitAt) and has instances for lists, Text and ByteString, among others.<br /><br />I also like to use <a href="http://hackage.haskell.org/package/monoid-subclasses-0.4.1.2/docs/Data-Monoid-Textual.html" rel="nofollow">TextualMonoid</a> whenever possible to make functions work with both String and Text.danidiazhttps://www.blogger.com/profile/00202497971909513168noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-15836081471287090822016-01-14T23:58:09.708+00:002016-01-14T23:58:09.708+00:00Thanks for the detailed analysis! Surprisingly int...Thanks for the detailed analysis! Surprisingly interesting for what initially appeared to be a boring function.<br /><br />I imagine the following generalisation to be useful:<br /><br /><b>chunksOfSizeBy :: (a -> Int) -> Int -> [a] -> [[a]]</b><br /><br />So that<br /><br /><b>chunksOfSize == chunksOfSizeBy length</b><br /><br />Then <b>chunksOfSizeBy id</b> makes sense too. I could even imagine applying it to <b>[Bool]</b> when we need to get chunks with no more than a certain number of ones or zeroes.<br /><br />The ultimate generalisation is, perhaps, as follows:<br /><br /><b>chunksOfSizeBy :: Real measure => (a -> measure) -> measure -> [a] -> [[a]]</b><br /><br />:)Andrey Mokhovhttps://www.blogger.com/profile/18314567019729554255noreply@blogger.com