Skip to content

Instantly share code, notes, and snippets.

@dmjio
Last active Nov 7, 2020
Embed
What would you like to do?
Mean, Standard Deviation, Variance in Haskell
module Main where
import Control.Applicative
{- Result = (Mean = 0.34, St. Dev = 0.115, Variance = 0.005) -}
main :: IO ()
main = interact stats
where
length' = fromIntegral . length
stats d = show (avg, stdDev, variance)
where
results :: [Float]
results = map read $ lines d
avg, stdDev :: Float
avg = (/) <$> sum <*> length' $ results
stdDev = sqrt $ summedElements / lengthX
where
lengthX = length' results
summedElements = sum (map (\x -> (x - avg) ^ 2) results)
variance = stdDev ^ 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment