Timothy
Timothy

Reputation: 91

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.

Upvotes: 6

Views: 86

Answers (1)

chi
chi

Reputation: 116139

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.

Upvotes: 7

Related Questions