RedHotChiliPepper

binarySearch .hs

Nov 28th, 2021 (edited)
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. binSearch :: Integral i => (i -> Bool) -> i -> i -> i
  2. binSearch f = go where
  3.     go l h
  4.         | l > h     = l
  5.         | f m       = go l (m - 1)
  6.         | otherwise = go (m + 1) h
  7.         where m = (l + h) `div` 2
  8.  
  9. binSearchA :: (a -> Bool) -> Array Int a -> Int
  10. binSearchA f a = binSearch (f . (a!)) l h where (l, h) = bounds a
Add Comment
Please, Sign In to add comment