tag:blogger.com,1999:blog-7094652.post6424426236236316335..comments2024-03-23T14:36:09.980+00:00Comments on Neil Mitchell's Blog (Haskell etc): Functional compositionNeil Mitchellhttp://www.blogger.com/profile/13084722756124486154noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-7094652.post-34100859641747254922007-07-25T04:03:00.000+01:002007-07-25T04:03:00.000+01:00I'd have usedlast . transpose . group . sortinstea...I'd have used<BR/>last . transpose . group . sort<BR/><BR/>instead. It handles the empty list and multiple mode cases better (by providing a list of results instead of a single one).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-31076042943845318892007-07-06T08:03:00.000+01:002007-07-06T08:03:00.000+01:00groupSet wouldn't really be in the lazy-evaluation...groupSet wouldn't really be in the lazy-evaluation spirit, I think. It would have to digest the whole list before emitting any output. "sort" has no choice but groupBy works even on infinite lists, which is cool.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-69620900964747510362007-06-30T00:17:00.000+01:002007-06-30T00:17:00.000+01:00@anonymous :> I'm not sure that I understand why >...@anonymous :<BR/>> I'm not sure that I understand why <BR/>> this feature should be considered so <BR/>> "addictive".<BR/><BR/>It's not just the composition, but the currying that makes this so nice. A brand new function that does what you want, built out of smaller pieces of polymorphic, general purpose functions.<BR/><BR/>I think the "maximumBy (comparing length)" bit is the sweetest of the bunch. maximumBy lets you hand in your own "value" function, appropriate for the list you wish to find a maximum in. "comparing" is itself a general purpose function that takes a list and compares all elements together, by whatever criteria you choose to give.<BR/><BR/><BR/>Finally, notice the function will also work over any list type that can be "ordered". <BR/><BR/>Sure, without composition the function definition has more paranetheses, but the other features at play - currying, polymorphism, and even "point-free" style - conspire to give a very readable and concise definition of the function "mostCommon".Justinhttps://www.blogger.com/profile/04138857652921543796noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-60981962652611091882007-06-29T20:23:00.000+01:002007-06-29T20:23:00.000+01:00@anonymous :> In fact, if maximumBy took its > arg...@anonymous :<BR/>> In fact, if maximumBy took its <BR/>> arguments in the opposite order, <BR/>> the above composition would not <BR/>> even have been possible.<BR/><BR/>then you'd do :<BR/><BR/>flip maximumByAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-18867203033876380072007-06-29T19:39:00.000+01:002007-06-29T19:39:00.000+01:00I'm not sure that I understand why this feature sh...I'm not sure that I understand why this feature should be considered so "addictive". In fact, I'm not sure why it is even in the language at all. The code above can be trivially expressed without the . operator simply by specifying a name for an argument. In fact, if maximumBy took its arguments in the opposite order, the above composition would not even have been possible. Perhaps I'm missing some critical functionality that this provides, but it seems to be one more unneeded way to express the same thing without imparting much additional clarity.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-58783010703873024142007-06-29T19:13:00.000+01:002007-06-29T19:13:00.000+01:00I made a small test in Javascript. Once you have t...I made a small test in Javascript. Once you have the functions defined, you can also call it rather nicely (although it's not nearly as cool as the haskell version).<BR/><BR/>You can call it like this:<BR/><BR/>alert("haskell".toArray().sort().group().maximumByLength()[0]);<BR/><BR/><A HREF="http://eidhof.nl/meuk/mostCommon.html" REL="nofollow">mostCommon.html</A>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-19920335648719881592007-06-29T18:53:00.000+01:002007-06-29T18:53:00.000+01:00lecamarade: that's for others to learn once they'v...lecamarade: that's for others to learn once they've been seduced by Haskell :)<BR/><BR/>chris: Thanks for including that. comparing is in GHC 6.6.1 in Data.Ord, but not in 6.4.Neil Mitchellhttps://www.blogger.com/profile/13084722756124486154noreply@blogger.comtag:blogger.com,1999:blog-7094652.post-90670793387494195742007-06-29T18:51:00.000+01:002007-06-29T18:51:00.000+01:00Nice :) I like these kind of functions too! To be ...Nice :) I like these kind of functions too! To be complete, here's a definition of comparing:<BR/><BR/>comparing f l r = compare (f l) (f r)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7094652.post-34824175127786846482007-06-29T17:32:00.000+01:002007-06-29T17:32:00.000+01:00Um ... you should add that the fact that many othe...Um ... you should add that the fact that <I>many</I> other languages don't have this feature (or, at least, lambda forms) renders them un-usable, when one is well-Haskellised.<BR/><BR/>These nifty features are what should carry a warning label: `ADDICTIVE! >8<'Anonymousnoreply@blogger.com