Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x in list for which f(x) is maximum

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.

like image 263
Timothy Avatar asked Oct 28 '17 13:10

Timothy


1 Answers

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.

like image 67
chi Avatar answered Sep 19 '22 12:09

chi