module Numeric.Statistics.Median (median, medianFast) where
import Data.List (sort)
median :: (Ord a, Fractional a) => [a] -> a
median x =
if odd n
then sort x !! (n `div` 2)
else ((sort x !! (n `div` 2 - 1)) + (sort x !! (n `div` 2))) / 2
where n = length x
medianFast :: (Ord a, Fractional a) => [a] -> a
medianFast [] = error "medianFast: empty list has no median"
medianFast zs =
let recurse (x0:_) (_:[]) = x0
recurse (x0:x1:_) (_:_:[]) = (x0+x1)/2
recurse (_:xs) (_:_:ys) = recurse xs ys
recurse _ _ =
error "median: this error cannot occur in the way 'recurse' is called"
in recurse zs zs