Created
March 14, 2015 15:33
-
-
Save m00nlight/18df8064dda4fcd3bf8c to your computer and use it in GitHub Desktop.
Haskell lowerBound and upperBound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Vector as V | |
import qualified Data.List as L | |
accSum :: Num t => [t] -> [t] | |
accSum xs = aux xs 0 | |
where aux [] a = [] | |
aux (x:xs') a = (a + x) : aux xs' (a + x) | |
lowerBound :: (Num a, Ord a) => V.Vector a -> a -> Int | |
lowerBound vec target = aux 0 ((V.length vec) - 1) | |
where | |
aux l r = if l > r then l | |
else let mid = (l + r) `div` 2 | |
in if vec V.! mid >= target then | |
aux l (mid - 1) | |
else aux (mid + 1) r | |
upperBound :: (Num a, Ord a) => V.Vector a -> a -> Int | |
upperBound vec target = aux 0 ((V.length vec) - 1) | |
where | |
aux l r = if l > r then l | |
else let mid = (l + r) `div` 2 | |
in if vec V.! mid > target then | |
aux l (mid - 1) | |
else aux (mid + 1) r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment