import Data.Int main :: IO () main = do let ls = [262734068,-1284266430,850241238,1854436759,925227574,1072391605, 1593098480,753973039,-1320460728,1653753372,1036510631,1372732547, 2027209822,1573664868, minBound, maxBound] print $ (== ls) $ map searchX ls searchX :: Int32 -> Int32 searchX x | p 0 > 0 = f minBound 0 | otherwise = f 0 maxBound where f :: Int32 -> Int32 -> Int32 f l r = case p m of 0 -> m 1 -> f l m -1 -> f (m + 1) r _ -> 0 where m = mid l r p = aboutX x aboutX :: Int32 -> Int32 -> Int32 aboutX x a = case compare a x of EQ -> 0 GT -> 1 LT -> -1 mid :: (Integral a) => a -> a -> a mid x y | signum x == signum y = (x + negate y) `div` 2 - negate y | otherwise = (x + y) `div` 2