I've been doing project euler problems in Haskell, and often I've found the need to get the value that gives the maximum of a function.
answer = argmaximum $ map collatzLength [1..10^6]
Where collatzLength is a previously defined function.
I came up with this code, which is heavily influenced by learning Ocaml. I run a helper function that has variables that keep track of the best value index and best index so far. It works perfectly for positive lists, but it seems like there's a better way.
Is there a less clunky way to implement argmaximum?
argmaximum :: (Ord a, Num a) => [a] -> a argmaximum lst = helper lst 0 0 0 where helper (x:xs) current bestVal bestIndex | current > bestVal = helper xs (current + 1) x current | otherwise = helper xs (current + 1) bestVal bestIndex helper null _ _ bestIndex = bestIndex