fork download
  1. import Data.Int
  2.  
  3. main :: IO ()
  4. main = do
  5. let ls = [262734068,-1284266430,850241238,1854436759,925227574,1072391605,
  6. 1593098480,753973039,-1320460728,1653753372,1036510631,1372732547,
  7. 2027209822,1573664868, minBound, maxBound]
  8. print $ (== ls) $ map searchX ls
  9.  
  10. searchX :: Int32 -> Int32
  11. searchX x
  12. | p 0 > 0 = f minBound 0
  13. where
  14. f :: Int32 -> Int32 -> Int32
  15. f l r =
  16. case p m of
  17. 0 -> m
  18. 1 -> f l m
  19. -1 -> f (m + 1) r
  20. _ -> 0
  21. where
  22. m = mid l r
  23. p = aboutX x
  24.  
  25. aboutX :: Int32 -> Int32 -> Int32
  26. aboutX x a =
  27. case compare a x of
  28. EQ -> 0
  29. GT -> 1
  30. LT -> -1
  31.  
  32. mid :: (Integral a) => a -> a -> a
  33. mid x y
  34. | signum x == signum y = (x + negate y) `div` 2 - negate y
  35. | otherwise = (x + y) `div` 2
  36.  
Success #stdin #stdout 0s 6216KB
stdin
Standard input is empty
stdout
True