Last active
August 29, 2015 14:23
-
-
Save gavinwhyte/3eb8fdfaf3da23a7cb1f to your computer and use it in GitHub Desktop.
Moving Average
This file contains 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
-- Computing the moving average | |
main = do | |
rawInput <- readFile "input.txt" | |
let input = clean rawInput | |
print input | |
putStrLn $ "mean is " ++ (show.mean) input | |
putStrLn $ "moving average is " ++ (show.avg) input | |
clean :: String -> [Double] | |
clean raw = map (\s -> read s :: Double) (lines raw) | |
avg :: [Double] -> Double | |
avg (x:xs) = a*x + (1-a)*(avg xs) | |
where a = 0.95 | |
avg [] = 0 | |
mean :: Fractional a => [a] -> a | |
mean xs = (sum xs) / (fromIntegral (length xs)) | |
-- [4.0,3.0,2.0,5.0,3.0,4.0,1.0,3.0,12.0,3.0] | |
-- mean is | |
-- moving average is |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment