Does the F # library have a standard function for `argMax`?

I am new to F # and have written a simple algorithm to get used to a language that requires argMax . Does the standard library have a function to search for a list item that maximizes the function? That is, if there is an existing function that behaves as follows:

 let argMax f xs = let rec go a fa zs = match zs with | [] -> a | z :: zs' -> let fz = fz if fz > fa then go z fz zs' else go a fa zs' match xs with | [] -> invalidArg "xs" "empty" | x :: xs' -> go x (fx) xs' 
+5
source share
1 answer

Yes, but it's called List.maxBy .

Here is an example:

 let fx = -(x * x) + 100 * x + 1000 List.maxBy f [0..1000] // val it : int = 50 f 50 // val it : int = 3500 

There is also List.minBy , and the same functions are available for Seq and Array .

+9
source

Source: https://habr.com/ru/post/1232913/


All Articles