Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 10, 2015 17:59
Show Gist options
  • Save haiiro-shimeji/4471629 to your computer and use it in GitHub Desktop.
Save haiiro-shimeji/4471629 to your computer and use it in GitHub Desktop.
histogram
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