Last active
December 10, 2015 17:59
-
-
Save haiiro-shimeji/4471629 to your computer and use it in GitHub Desktop.
histogram
This file contains hidden or 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 Data.List (sort) | |
-- make histogram | |
-- | |
-- @param xs [Double] Data array | |
-- @param delta Double histogram's step width. | |
-- @param pivot Double | |
-- | |
histogram :: Double -> Double -> [Double] -> [(Double,Int)] | |
-- sortすると遅くなりそうだけど、再代入できないからこんな感じなのかな | |
-- 最初にrangeごとにカウンターを用意して、数えていくほうが計算量が少なそう | |
histogram delta _ _ | |
| 0 >= delta = error "delta must be >0." | |
histogram delta pivot xs = (reverse (make' pivot (-delta) (reverse a))) ++ (make' pivot delta b) | |
where | |
(a, b) = break (> pivot) $ sort xs | |
make' _ _ [] = [] | |
make' d inc xs = (d, length a) : make' (d + inc) inc b | |
where | |
comp | |
| 0 > inc = (>) | |
| otherwise = (<) | |
(a, b) = break (comp d) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment