Skip to content

Instantly share code, notes, and snippets.

@gavinwhyte
Last active August 29, 2015 14:23
Show Gist options
  • Save gavinwhyte/3eb8fdfaf3da23a7cb1f to your computer and use it in GitHub Desktop.
Save gavinwhyte/3eb8fdfaf3da23a7cb1f to your computer and use it in GitHub Desktop.
Moving Average
-- 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