Created
June 6, 2018 16:38
-
-
Save skalahonza/dab9d86b3f6c067589c4c2b6431ba648 to your computer and use it in GitHub Desktop.
Basic statistic means in haskell.
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
| harmonicMean :: [Double] -> Double | |
| harmonicMean list = (doubleLength list) / sumo where | |
| sumo = (sum twisted) | |
| twisted = (map inv list) | |
| geometricMean :: [Double] -> Double | |
| geometricMean list = (product list) ** (1/(doubleLength list)) | |
| arithmeticMean :: [Double] -> Double | |
| arithmeticMean list = (sum list) / (doubleLength list) | |
| quadraticMean :: [Double] -> Double | |
| quadraticMean list = (sqrt quadr) where | |
| powered = (map pow2 list) | |
| quadr = (sum powered) / (doubleLength list) | |
| doubleLength (x:xs) = 1.0 + (doubleLength xs) | |
| doubleLength [] = 0.0 | |
| -- a na b | |
| power a 1 = a | |
| power a 0 = 1 | |
| power a b = a * (power a (b-1)) | |
| pow2 x = x*x | |
| inv x = 1/x | |
| mean :: String -> [Double] -> Double | |
| mean variant | |
| | variant == "harmonic" = harmonicMean | |
| | variant == "geometric" = geometricMean | |
| | variant == "arithmetic" = arithmeticMean | |
| | variant == "quadratic" = quadraticMean | |
| -- code from the lectures | |
| split :: String -> Char -> [String] | |
| split [] _ = [""] | |
| split (x:xs) c = let rest = split xs c in if x == c then "" : rest else (x : head rest) : tail rest | |
| doubleparse :: String -> Double | |
| doubleparse s = (read s :: Double) | |
| stringToDOubles :: String -> [Double] | |
| stringToDOubles s = (map doubleparse (split s ' ')) | |
| makeMean cisla typ = mean typ (stringToDOubles cisla) | |
| main = do | |
| cisla <- getLine | |
| typ <- getLine | |
| putStrLn (show (makeMean cisla typ)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment