Skip to content

Instantly share code, notes, and snippets.

@dmjio
Last active November 7, 2020 21:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmjio/3426797 to your computer and use it in GitHub Desktop.
Save dmjio/3426797 to your computer and use it in GitHub Desktop.
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