I am new to F#. Is the following code, which represents about three hours of work, a canonical way to achieve binary search? If not, how can it be improved for readability and maintainability?
let binarySearch (array: 'a) (target: 'a) = let rec search min max = match (min, max) with | (min, max) when max < min -> -1 | _ -> let guess = ((min + max) / 2); match array.[guess] with | x when x = target -> guess | x when x < target -> search (guess + 1) (max) | _ -> search (min) (guess - 1) search (0) (array.Length - 1)
Aspects that I already like:
- hiding the recursive function within the non-recursive function.
- using pattern matching instead of
if-else-then, based on suggestions here,
- accepting generic types with