I've been having this problem a lot recently in some form or another. Lets say that I want to take a number x and apply a bunch of high order functions to x producing y. I then check to see if y satisfies a certain property, if it does, then I want to return x.
This problem seems to get really tricky when I have a list of numbers [x1,x2..xn] and the functions I apply condense the list. For example, I apply a function to each of the elements in the list (producing [y1,y2..]), sort, group, and then I want to return the values of x for the largest group. For example:
head . reverse . sort . map (length) . group . sort . map (mod 4) $ [1..10]
The answer is 6, but how would I rewrite a function like this to tell me which elements numbers 1 through 10 belong to those 6?
I've played with the idea of passing tuples and using fst everywhere until the snd is required, or writing a new class to make it so something like sort only works on one element of the class, but I can't seem to come up with a clean approach
Thanks for the help.
Here's a small set of changes to your code that return the value rather than the length; the use of the *By functions avoids the need to use tuples:
maximumBy (compare `on` length) . groupBy ((==) `on` mod 4) . sortBy (compare `on` mod 4) $ [1..10]
This code requires Data.List and Data.Function. Data.Function includes on, which allows a comparison (for sorting or grouping) to be applied to some function of the input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With