I am trying to find an x
in a list for which score x
has the maximum value. I tried snd (maximum [(score x, x) | x <- codes])
which works, but I was wondering whether there were a faster way to do this, without actually storing both the function and the value.
Your solution is fine. If you want some library help, you can use
maximumBy (comparing score) codes
Note that this, compared to your code, will perform more calls to score
. If score
is expensive to compute, your approach is better since it will compute score
only once per list element.
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