Given an Array
, I need to compute the index of the maximum element (which is assumed to be unique).
My current attempt is:
maxIndex' :: (Ix i, Ord a) => Array i a -> [i] -> i -> a -> i -> a -> i
maxIndex' arr ids index element maxIndex maxElement
| length ids == 1 && element > maxElement = index
| length ids == 1 && element <= maxElement = maxIndex
| element > maxElement = maxIndex' arr (tail ids) (head ids) (arr ! head ids) index element
| otherwise = maxIndex' arr (tail ids) (head ids) (arr ! head ids) maxIndex maxElement
maxIndex :: (Ix i, Ord a) => Array i a -> i
maxIndex arr = maxIndex' arr (tail (indices arr)) firstIndex firstMaximum firstIndex firstMaximum
where
firstIndex = head (indices arr)
firstMaximum = arr ! firstIndex
The current implementation, however, is incorrect:
print (maxIndex (array (False,True) [(False,54),(True,104)])) -- Prints False.
print (maxIndex (array (False,True) [(True,104),(False,54)])) -- Prints False.
So, what am I missing?